DP(最长公共子序列)--Common Subsequence
2018-03-14 20:44
295 查看
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
Sample Output
代码:
emmmmm模板题
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<stdio.h> #include<algorithm> #include<iostream> #include<string> #include<string.h> using namespace std; char a[1005],b[1005]; int s[1005][1005],m,n,i,j; int main() { while(cin>>a>>b){ m=strlen(a); n=strlen(b); for(i=0;i<m;i++) for(j=0;j<n;j++) { if(a[i]==b[j]) s[i+1][j+1]=s[i][j]+1; else s[i+1][j+1]=max(s[i][j+1],s[i+1][j]); } printf("%d\n",s[m] ); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(s,0,sizeof(s)); } }
emmmmm模板题
相关文章推荐
- poj1458 Common Subsequence (dp,最长公共子序列)
- (Relax DP1.6)UVA 10405 Longest Common Subsequence(使用DP来求解最长公共子序列LCS)
- hdu1159 Common Subsequence 最长公共子序列 经典DP
- hdu 1159 Common Subsequence(最长公共子序列 DP)
- POJ 1458 Common Subsequence-dp-(最长公共子序列模型)
- 最长公共子序列(dp) & hdu 1159 Common Subsequence
- DP动态规划——hdu 1008 Common Subsequence(最长公共子序列)
- HDU 1159 Common Subsequence (求两个串的最长公共子序列 dp)
- HDU 1159 Common Subsequence--DP--(最长公共子序列)
- 【dp专题1】F - Common Subsequence 【最长公共子序列模板LCS】hdu1159
- POJ 1458-Common Subsequence(dp之最长公共子序列)
- 最长公共子序列DP Common Subsequence HDU 1159
- Common Subsequence DP最长公共子序列
- hdu 1159 Common Subsequence(最长公共子序列,DP)
- HDU 1159 Common Subsequence(最长公共子序列)dp
- POJ 1458 Common Subsequence DP LCS 最长公共子序列
- PKU 1458 Common Subsequence(最长公共子序列,dp,简单)
- hdu 1159 Common Subsequence(DP最长公共子序列)
- HDUOJ 1159 - Common Subsequence(DP:最长公共子序列LCS)
- DP动态规划——hdu 1008 Common Subsequence(最长公共子序列)