Poj1458--dp,lcscommon subsequence
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 40529 |
|
Accepted: 16351 |
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, Xij = 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
Test instructions: Ask for LCS, water problem!
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=1000100;CharS[MAXN],T[MAXN];intdp[2][MAXN];intMain () { while(SCANF ("%s%s", s,t)! =EOF) { intLs=strlen (s), lt=strlen (t); Memset (DP,0,sizeof(DP)); Char*ss=s-1, *tt=t-1; Memset (DP,0,sizeof(DP)); for(intI=1; i<=ls;i++){ for(intj=1; j<=lt;j++){ if(Ss[i]==tt[j]) dp[i%2][j]=dp[(i+1)%2][j-1]+1; Elsedp[i%2][j]=max (dp[(i+1)%2][j],dp[i%2][j-1]); }} printf ("%d\n", dp[ls%2][lt]); } return 0;}View Code
Poj1458--dp,lcs