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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; #define met(a,b) (memset(a,b,sizeof(a)))
#define N 1100
#define INF 0xffffff char s1[N], s2[N];
int dp[N][N]; int main()
{
while(scanf("%s%s", s1, s2)!=EOF)
{
int i, j, len1=strlen(s1), len2=strlen(s2); met(dp, ); ///要考虑下下标越界的问题
for(i=; i<=len1; i++)
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 (poj 1458)的更多相关文章

  1. 最长公共子序列(Longest common subsequence)

    问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子序列.(子序列中的字符不要求连续) 这道题可以 ...

  2. UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...

  3. 算法实践--最长公共子序列(Longest Common Subsquence)

    什么是最长公共子序列 X=ACCG Y=CCAGCA 长度为1的公共子序列: {A} {C} {G} 长度为2的公共子序列:{AC} {CC} {CG} {AG} 长度为3的公共子序列:{ACG} 长 ...

  4. HDU 1159 Common Subsequence(POJ 1458)

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

  5. Common Subsequence POJ - 1458 最长公共子序列 线性DP

    #include <iostream> #include <algorithm> #include <string> #include <cstring> ...

  6. 算法练习——最长公共子序列的问题(LCS)

    问题描述: 对于两个序列X和Y的公共子序列中,长度最长的那个,定义为X和Y的最长公共子序列.X  Y   各自字符串有顺序,但是不一定需要相邻. 最长公共子串(Longest Common Subst ...

  7. [Python]最长公共子序列 VS 最长公共子串[动态规划]

    前言 由于原微软开源的基于古老的perl语言的Rouge依赖环境实在难以搭建,遂跟着Rouge论文的描述自行实现. Rouge存在N.L.S.W.SU等几大子评估指标.在复现Rouge-L的函数时,便 ...

  8. 最长公共子串(Longest common substring)

    问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...

  9. Common Subsequence--poj1458(最长公共子序列)

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

随机推荐

  1. Vs2013 如何使用EF6来连接mysql数据库

    参考文章: 1.http://www.dotblogs.com.tw/yc421206/archive/2014/03/14/144395.aspx 参考了1,在VS中算是已经配置好了,在添加数据实体 ...

  2. const修饰符用法

    1. 将一个对象设置为不可修改 const int a = 100; 2. 指向const对象的指针 const int* p = 3;可以通过指针来修改指针所指向的值,但是不能通过指针*p修改对像的 ...

  3. Android Studio 解析json文件出现中文乱码解决方法

    作为一个Android开发初学者,好不容易找到解决方法,跟大家分享一下, 其实很简单,只要保持服务器上的文件(date2.json)与软件的编码方式一样就行. 我用的Android Studio是ut ...

  4. Oracle_高级功能(3) synonym和database link

    一.同义词synonymconnect sys/123 as sysdba;select * from emp;ORA-00942: 表或视图不存在create synonym emp for sco ...

  5. PAT 1085 PAT单位排行(25)(映射、集合训练)

    1085 PAT单位排行(25 分) 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​5​​),即考生人数.随 ...

  6. TCP Nagle算法&&延迟确认机制

    TCP Nagle算法&&延迟确认机制 收藏 秋风醉了 发表于 3年前 阅读 1367 收藏 0 点赞 0 评论 0 [腾讯云]买域名送云解析+SSL证书+建站!>>> ...

  7. __PRETTY_FUNCTION__, __FUNCTION__, __func__

    __PRETTY_FUNCTION__, __FUNCTION__, __func__这三者的区别是什么? http://stackoverflow.com/questions/4384765/wha ...

  8. 为什么CPU的主频止步于4GHz?

    你对CPU的认识大概还停留在奔腾4年代吧……奔腾4最终止步于3.8GHz,原计划推出的4GHz奔腾4处理器也被胎死腹中.英特尔意识到处理器研发道路上走入了“唯主频论”的误区,2004年10月,英特尔总 ...

  9. spring batch批处理框架学习

    内如主要来自以下链接: http://www.importnew.com/26177.html http://www.infoq.com/cn/articles/analysis-of-large-d ...

  10. Luogu 4556 雨天的尾巴 - 启发式合并线段树

    Solution 用$col$记录 数量最多的种类, $sum$记录 种类$col$ 的数量. 然后问题就是树上链修改, 求 每个节点 数量最多的种类. 用树上差分 + 线段树合并更新即可. Code ...