题目链接:http://poj.org/problem?id=1458

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 55099   Accepted: 22973

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

 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; char a[MAXN], b[MAXN];
int dp[MAXN][MAXN]; int main()
{
while(scanf("%s%s", a+, b+)!=EOF)
{
int n = strlen(a+);
int m = strlen(b+); ms(dp, );
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(a[i]==b[j])
dp[i][j] = dp[i-][j-]+;
else
dp[i][j] = max(dp[i][j-], dp[i-][j]);
}
printf("%d\n", dp[n][m]);
}
}

最新文章

  1. android Bundle savedInstanceState用途
  2. IE下new Date不支持传参数的解决
  3. hdu 1272 小希的迷宫 解题报告
  4. 【BZOJ】2178: 圆的面积并
  5. AWR快照管理
  6. MySQL中的WITH ROLLUP
  7. 单片机串口通讯RXD与TXD如何对接详解
  8. 在.NET下学习Extjs(第四个案例 Extjs扩展的原理)
  9. Python中的列表解析和生成表达式
  10. 广域网佰腾玩O2O笑话——它看起来很漂亮,注定不低于
  11. 【iOS发展-28】制造业UITabBarController标记控制器、定制UITabBarItem文字图像6途径和More评论
  12. IOS学习之路七(通过xib自定义UITableViewCell)
  13. [编织消息框架][JAVA核心技术]动态代理应用10-水平扩展方案
  14. Android官方命令深入分析之dmtracedump
  15. codeforces 13 D
  16. spring boot 如何添加拦截
  17. 由ngx.say和ngx.print差异引发的血案
  18. Java中输出正则表达式匹配到的内容
  19. Spark入门——什么是Hadoop,为什么是Spark?
  20. Redis学习第八课:Redis高级实用特性(一)

热门文章

  1. ES6(字符串)
  2. 自动化测试-selenium IDE使用
  3. zoj 1240
  4. centos 7 下vnc弹出窗口太小解决方法
  5. Codeforces932D. Tree
  6. CERC 2014 (动态树+主席树)
  7. 《TCP/IP详解卷1:协议》——第1章:概述(转载)
  8. Search in Rotated Sorted Array(二分查找)
  9. Windows平台kafka环境的搭建
  10. foobar2000实现用手机远程控制PC命令行版