Common subsequencetime limit:2000/1000
MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 28494 Accepted Submission (s): 12735
Problem Descriptiona 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 E Xists 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.
The program input was from a text file. Each data set in the file contains the strings representing the given sequences. The sequences is separated by any number of white spaces. The input data is correct. 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
A very basic LCS. The following is a DP route for the first case, such as. Hope can help everyone.
AC Code:
#include <stdio.h> #include <string.h> #define MAX (A, B) (a>b?a:b) Char a[1000],s[1000];int dp[1000][1000 ];int Main () { int i,j,k; while (scanf ("%s%s", A,s)!=eof) { memset (dp,0,sizeof (DP)); int L=strlen (a); int Le=strlen (s); for (i=1;i<=l;i++) {for (j=1;j<=le;j++) if (a[i-1]==s[j-1])//To determine whether the left and the upper side characters are equal dp[i][j]=dp[ i-1][j-1]+1;//the upper left side of the DP value +1 else Dp[i][j]=max (dp[i-1][j],dp[i][j-1]),//Take the left or the upper side of the maximum DP value } printf ( "%d\n", Dp[l][le]); } return 0;}
Copyright Notice: Original article, there is a reference to the place, a lot of support. Hhh
Hdoj 1159 Common subsequence "LCS" "DP"