F-fTime
limit:1000MS
Memory Limit:10000KB
64bit IO Format:%i64d &%i64 U
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 the Re exists a strictly increasing sequence < I1, I2, ..., ik > of indices of X such so for all J =,..., k, X ij = Zj. For example, Z = < A, b, F, C > are a subsequence of X = < A, b, C, F, B, C > with index sequence < 1, 2, 4, 6;. Given 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 was from the STD input. Each data set in the input contains the strings representing the given sequences. The sequences is separated by any number of white spaces. The input data is correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from th e beginning of a separate line.
Sample Input
ABCFBC abfcabprogramming contest ABCD MNP
Sample Output
420
The puzzle: give you two strings to find their longest common subsequence. This problem has been done many times, although added something, or not write right, or the LCS understand enough.
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Chara[1100],b[1100];intdp[1100][1100];intMain () { while(SCANF ("%s%s", A, b)! =EOF) { intx=strlen (a); inty=strlen (b); for(intI=1; i<=x;i++) { for(intj=1; j<=y;j++) { if(a[i-1]==b[j-1]) Dp[i][j]=dp[i-1][j-1]+1; ElseDp[i][j]=max (dp[i-1][j],dp[i][j-1]); }} cout<<dp[x][y]<<Endl; } return 0;}
Week F question POJ 1458 (longest common sub-sequence)