1808: Common sub-sequence
- View
- Submit
- Statistics
- Questions
-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
Describe
-
We call sequence Z = < Z1, Z2, ..., ZK > is sequence x = < X1, x2, ..., XM > Subsequence when and only if there is a
strictly ascending sequence < I1, I2, ..., ik > , making it to J = 1, 2, ..., k, with Xij = ZJ. For example z = < A, b, F, C > is the subsequence of x = < A, b, C, F, B, C >.
Now given two sequences x and Y, your task is to find the largest common subsequence of x and Y, that is to find the longest sequence z, so that Z is both a subsequence of X and a sub-sequence of Y.
-
Input
-
The input includes multiple sets of test data. Each group of data consists of one row, giving two strings with a length of not more than 200, representing two sequences. Two strings are separated by a number of spaces.
-
Output
-
For each set of input data, output one row, giving the length of the maximum common subsequence of two series.
-
Sample input
-
ABCFBC abfcabprogramming contest ABCD MNP
-
Sample output
-
420
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Charx[1001],y[1001];intc[1001][1001];intL1,l2;intMain () { while(SCANF ("%s%s", x+1, y+1)==2) {memset (c,0,sizeof(c)); L1=strlen (x+1); L2=strlen (y+1); for(intI=1; i<=l1;i++) for(intj=1; j<=l2;j++) if(x[i]==Y[j]) c[i][j]=c[i-1][j-1]+1; ElseC[i][j]=max (c[i-1][j],c[i][j-1]); printf ("%d\n", C[l1][l2]);} return 0;}
1808: Common sub-sequence (common subsequence)