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, x ij = 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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm> using namespace std; int main()
{
char a[1005],b[1005];
int dp[1005][1005];
while(scanf("%s%s",a,b)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int t=1;t<=strlen(a);t++)
{
for(int j=1;j<=strlen(b);j++)
{
if(a[t-1]==b[j-1])
{
dp[t][j]=max(dp[t-1][j-1]+1,dp[t][j]);
}
else
{
dp[t][j]=max(dp[t-1][j],dp[t][j-1]);
}
}
}
printf("%d\n",dp[strlen(a)][strlen(b)]);
} }

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

  1. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

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

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  3. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  4. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  5. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

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

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

  7. LCS修改版(Longest Common Subsequence 最长公共子序列)

    题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉 ...

  8. POJ 1458 Common Subsequence 最长公共子序列

    题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...

  9. LCS(Longest Common Subsequence)最长公共子序列

    最长公共子序列(LCS)是一个在一个序列集合中(通常为两个序列)用来查找所有序列中最长子序列的问题.这与查找最长公共子串的问题不同的地方是:子序列不需要在原序列中占用连续的位置 .最长公共子序列问题是 ...

  10. POJ 1458 Common Subsequence 最长公共子序列 LCS

    LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> ...

随机推荐

  1. sql 的积累

    sql的积累 By:山高似水深 原创 转载注明出处 .REVERSE() 反转 例如: Hive 可用 2016年12月3日11:31:59 2.instr(str,'.')位置 结果:得出在str中 ...

  2. linux下vtune使用

    安装:http://www.cnblogs.com/jiu0821/p/5943533.html 终端输入amplxe-gui,打开vtune界面. 点击new project,进入project p ...

  3. 在PyCharm 软件中设置你的项目 使用的Python版本

    在PyCharm 软件中设置你的项目 使用的Python版本 python2 和 python3 有很大的不同,使用python2 编写的程序,如果使用python3 就运行不了:使用python3编 ...

  4. Linux alien命令

    一.简介 alien是一个用于在各种不同的Linux包格式相互转换的工具,其最常见的用法是将.rpm转换成.deb(或者反过来). 二.安装 http://toutiao.com/a618899776 ...

  5. 4、Brief primer and lexicon for PacBio SMRT sequencing

    转载:http://pacbiofileformats.readthedocs.io/en/5.1/Primer.html 转载:http://pacbiofileformats.readthedoc ...

  6. Luogu 1357 花园

    发现$m$很小,直接状压起来,可以处理出一开始的合法的状态. 对于每一个合法的状态,可以处理出它的转移方向,即在后面填一个$1$或者填一个$0$,反着处理比较方便. 考虑一下环的情况,在这题中有一个小 ...

  7. easyUI datagrid 分页参数page和rows

    Struts2获取easyUI datagrid 分页参数page和rows 用pageHelper分页时,只要是能够获取前台传来的两个参数page和rows基本就完成了很大一部分. 获取方法:定义两 ...

  8. Proxool Provider unable to load JAXP configurator file: proxoolconf.xml

    Proxool Provider unable to load JAXP configurator file: proxoolconf.xml log4j:WARN No appenders coul ...

  9. C++面试笔记--const、sizeof

    首先来一个关于const的全面的解释,先看一波代码,之后再进行详细的分情况解释 ; const int *a=&b;//指向一个int常量的指针 int const *a=&b;//和 ...

  10. TinkerPop中的遍历:图的遍历步骤(3/3)

    48 Project Step project() 步骤(map)将当前对象投射到由提供的标签键入的Map<String,Object>中. gremlin> g.V().out(' ...