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 ;
}
这是个模板记住就行,欢迎各位留言询问!

最新文章

  1. MongoDB学习笔记~数据结构与实体对象不一致时,它会怎么样?
  2. cloudera learning4:Hadoop集群规划
  3. hdu1161 欧拉路
  4. python2.x和3.x的区别
  5. SWAP空间不足,如何进行添加
  6. 快捷下载 sourceForge下的资源
  7. Merge Sort
  8. jquery获取显示器,屏幕的宽度,和高度
  9. 【原创】微信公众号与HTML 5混合模式揭秘3——JSSDK获取地理位置
  10. 【hdu2896】病毒侵袭
  11. sql server 分布式事务
  12. Redis配置文件分析
  13. 9_Permanent Storage
  14. Drying(贪心)
  15. HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP
  16. 表空间tablespace,数据文件datafiles,和控制文件control files介绍
  17. 工具包分享-常用工具。by-某某
  18. 微信小程序--代码构成---JS 交互逻辑
  19. Dynamics 365 CRM 添加自定义按钮
  20. Java用户名登录学生信息管理系统并对其进行增删改查操作

热门文章

  1. iOS学习之数据请求
  2. vi编辑器经典技巧 -备
  3. Kill 锁,1222:已超过了锁请求超时时段,
  4. cf445B DZY Loves Chemistry
  5. Windows下编译eXosip、osip,以及UAC和UAS的例子
  6. Java里的equals总结
  7. hdu5045:带权二分图匹配
  8. usaco5.5-Hidden Passwords
  9. C# - 创建List属性的简单方法
  10. swift 随机数