Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 43211   Accepted: 17526

Description

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.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

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.

Sample Input

abcfbc         abfcab
programming contest
abcd mnp

Sample Output

4
2
0

Source

 
 
 
 
这个题是求最大公共子序列的长度,不是公共子串,他们的区别是::最长公共子串必须是连续的,而最长公共子序列不需要连续!
 
 
注意最长公共子串(Longest CommonSubstring)和最长公共子序列(LongestCommon Subsequence, LCS)的区别:子串(Substring)是串的一个连续的部分,子序列(Subsequence)则是从不改变序列的顺序,而从序列中去掉任意的元素而获得的新序列;更简略地说,前者(子串)的字符的位置必须连续,后者(子序列LCS)则不必。比如字符串acdfg同akdfc的最长公共子串为df,而他们的最长公共子序列是adf。
 
 
 
这个题就是个裸的cls
 
 
 
我简单说一下cls的原理
 
 
就是要让任意一个串每次增加一个字符分别和另一个串比较,求出最大的公共部分!
 
 
 
 

LCS(S1,S2)等于下列3项的最大者:

(1)LCS(S1,S2’)--如果C1不等于C2;

(2)LCS(S1’,S2)--如果C1不等于C2;

(3)LCS(S1’,S2’) LCS(S1',S2')+1--如果C1等于C2;

边界终止条件:如果S1和S2都是空串,则结果也是空串。

 
 
看张图好理解
 
 
 
 
先让A自己是一个串,和串BDCABA一个一个比
 
第一次比较 A和B他们都是一个串,末尾A和B不相等,最长的公共部分就是A前面串(空)和B或者B前面串(空)和A的最大一个,显然他们前面都没有,就是0
 
 
 
第二次比较A和 BD,末尾A和D不相等,最长的公共部分就是A前面串(空)和BD或者D前面串(B)和A的最大一个,显然他们公共部分为0
 
 
 
第三次比较A和BDC,末尾A和C不相等,最长的公共部分就是A前面串(空)和BDC或者C前面串(BD)和A的最大一个,显然他们公共部分为0
 
 
 
第四次比较A和BDCA,末尾A和A相等,最长的公共部分就是A前面串(空)和A前面串(BDC)公共长度加1(因为最后一个相等都是A)显然他们公共部分为1
....
 
 
 
依次写到末尾就是最长的公共部分的最大长度
 
 
 
 
套用模板:
 
 
 
 
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s1[],s2[];
int dp[][];
int main()
{
int i,j;
while(scanf("%s%s",s1,s2)!=EOF)
{
memset(dp,,sizeof(dp));
int len1=strlen(s1);
int len2=strlen(s2);
for(i=;i<=len1;i++)//i和j从1开始整体右下移一位,避开串钱为空的情况
{
for(j=;j<=len2;j++)
{
if(s1[i-]==s2[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i][j-],dp[i-][j]);
}
}
printf("%d\n",dp[len1][len2]);
}
return ;
}
这是个模板记住就行,欢迎各位留言询问!

Common Subsequence--poj1458(最长公共子序列)的更多相关文章

  1. hdu 1159 Common Subsequence(LCS最长公共子序列)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. hdu 1159 Common Subsequence (最长公共子序列 +代码)

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  3. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  4. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  5. POJ 1458 Common Subsequence 【最长公共子序列】

    解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序 ...

  6. POJ 1458 Common Subsequence(最长公共子序列)

    题目链接Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description A subsequence o ...

  7. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

  8. hdu 1159 Common Subsequence(最长公共子序列,DP)

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

  9. POJ1458 Common Subsequence 【最长公共子序列】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37614   Accepted: 15 ...

  10. POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)

    题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Tot ...

随机推荐

  1. IOS 如何选择delegate、notification、KVO?

    IOS 如何选择delegate.notification.KVO? 博客分类: IOS   前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有 ...

  2. 从现在开始使用nodejs开发的几点答疑

    1.为什么要开始用nodejs, 而不是php 理由有三点: 因为我是前端程序员出身,nodejs都是用javascript写的,这样相当于前端和后端都使用javascript,开发更加有效率.当然很 ...

  3. sqlserver 数据库里面金额类型为什么不建议用float,实例告诉你为什么不能。

    项目当中如果设计到金额类型的数据,你是否有考虑过为什么不能用float类型. 这里举个例子: DECLARE @price1 FLOAT; SET @price1 = 1; SET @price1 = ...

  4. 测试一下PHP官方的新一代PHP加速插件ZendOpcache的性能及配置

    过程不表,都比较顺利 参考如下URL: http://www.lvtao.net/server/ZendOpcache.html 大家知道目前PHP的缓存插件一般有三个:APC.eAccelerato ...

  5. Codeforces 509C Sums of Digits

    http://codeforces.com/contest/509/problem/C  题目大意: 给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方 ...

  6. LeetCode_Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  7. libeXosip2(3) -- SIP messages and call control API

    SIP messages and call control API The SIP messages and call control API. More... Modules eXosip2 INV ...

  8. 用c++编写一个不能被继承的类(但是可以在类外部定义该类的对象)

    据我们知道,我们只要把类的构造函数和析构函数定义为private类型,那么就不能够在外部建立给类的对象,也就不能以给类为基类进行继承,因为如果继承,建立对象的时候将要调用基类的构造函数,但是因为为pr ...

  9. 第31讲 UI组件之 Gallery画廊控件

    第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...

  10. 转:TestLink1.9.3测试用例:Excel转换XML工具<二>实现代码

    TestLink1.9.3测试用例:Excel转换XML工具<二>实现代码 http://blog.csdn.net/candle806/article/details/7490599 以 ...