Problem description
We call the sequence Z = < Z1, Z2, ..., ZK > is sequence x = < X1, x2, ..., the subsequence of XM > When and only if there is a strictly ascending sequence < I1, I2, ..., IK, make to J = 1, 2, ..., K, There is 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 data
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 requirements
For each set of input data, output one row, giving the length of the maximum common subsequence of two series.
Input sample
ABCFBC Abfcab
Programming Contest
ABCD MNP
Output sample
4
2
0
Import Java.util.Scanner; Public classcommonsubsequence { Public Static void Main(string[] args) {intDp[][] =New int[ -][ -]; Scannerinch=NewScanner (System.inch); String A =inch. Next (); String B =inch. Next ();intA = A.length ();intb = B.length (); for(inti =1; I <= A; i++) { for(intj =1; J <= B; J + +) {if((A.charat (i1) -' 0 ') = = (B.charat (J-1) -' 0 ') {Dp[i][j] = dp[i-1][j-1] +1; }Else{Dp[i][j] = Math.max (Dp[i-1][J], Dp[i][j-1]); }}} System. out. println (Dp[a][b]); }}
Longest common sub-sequence