YTU 2402: Common Subsequence
2402: Common Subsequence
时间限制: 1 Sec 内存限制: 32 MB
提交: 63 解决: 33
题目描述
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly
increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the
length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard
output the length of the maximum-length common subsequence from the beginning of a separate line.
输入
abcfbc abfcab
programming contest
abcd mnp
输出
4
2
0
样例输入
abcfbc abfcab
programming contest
abcd mnp
样例输出
4
2
0
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char s1[1000],s2[1000];
int dp[1000][1000];
int len1,len2;
void LCS()
{
int i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=len1; i++)
{
for(j = 1; j<=len2; j++)
{
if(s1[i-1] == s2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
int main()
{
while(~scanf("%s%s",s1,s2))
{
len1 = strlen(s1);
len2 = strlen(s2);
LCS();
printf("%d\n",dp[len1][len2]);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char s1[1000],s2[1000];
int dp[1000][1000];
int len1,len2;
void LCS()
{
int i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=len1; i++)
{
for(j = 1; j<=len2; j++)
{
if(s1[i-1] == s2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
int main()
{
while(~scanf("%s%s",s1,s2))
{
len1 = strlen(s1);
len2 = strlen(s2);
LCS();
printf("%d\n",dp[len1][len2]);
}
return 0;
}
YTU 2402: Common Subsequence的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- Common Subsequence LCS
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Description A subsequ ...
- poj 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46387 Accepted: 19 ...
- Longest Increasing Common Subsequence (LICS)
最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...
随机推荐
- Python中的*arg和**kwarg
一个简单的函数 首先我们可以定一个简单的函数, 函数内部只考虑required_arg这一个形参(位置参数) def exmaple(required_arg): print required_arg ...
- ubuntu samba 配置简介
Ubuntu 11.04下虚拟机Samba的共享配置详细步骤 一. Ubuntu 11.04下Samba的安装: $ sudo apt-get insall samba ...
- tree(poj 1741)
题意:给一颗n个节点的树,每条边上有一个距离v(v<=1000).定义d(u,v)为u到v的最小距离.给定k值,求有多少点对(u,v)使u到v的距离小于等于k. /* 照着点分治模板敲了敲,有很 ...
- js闭包的用途[转载]
通过使用闭包,我们可以做很多事情.比如模拟面向对象的代码风格:更优雅,更简洁的表达出代码:在某些方面提升代码的执行效率. 1 匿名自执行函数我们知道所有的变量,如果不加上var关键字,则默认的会添加到 ...
- 用Google Analytics UTM标注社会化媒体分享流量来源
随着社会化媒体营销概念近两年的日益盛行,敢于吃螃蟹的营销工作者们展开了一些尝试,发现对社会化营销效果进行综合评估是一大难点,价值难以衡量.主要原因在于它的营销效果中混杂了直接的目标转化.品牌宣传.品牌 ...
- hdu - 5007 Post Robot (水题)
http://acm.hdu.edu.cn/showproblem.php?pid=5007 #include<iostream> #include<stdio.h> #inc ...
- ViewPager与Fragment刷新数据
唉,每次都忘记怎么写,还是老老实实记一下吧! public class MDTabViewPagerAdapter extends FragmentStatePagerAdapter { privat ...
- 2018.11.5 PION模拟赛
期望:30 + 40 + 50 = 120 实际:30 + 50 + 40 = 120 ‘’ 思路:最重要的是发现 是完全没有用的,然后这个题目就可以转成DP来做. /* 期望的分:30 */ #in ...
- js如何获取一个object的第一个数据
var obj = { "1":"123", "2":"456" } console.info( obj[Object. ...
- java大文件的分割和合并
原文:http://www.open-open.com/code/view/1441679313430 import java.io.File; import java.io.FileInputStr ...