Common subsequence
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 26493 Accepted Submission (s): 11771
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
said that a few days did not have a problem, the reasons for a lot of courses, usually many die, provincial training also often run west side, do the topic is no longer water problem, the main learning process quite time, I believe as long as the algorithm learned, and then
A similar problem would not be a waste of time. Today 51 is also a little bit of their own time, (in fact, 51 still have a lot of things to do).
refer to this blog post on the Internet, and the introduction of the algorithm is described in the same
http://blog.csdn.net/yysdsyl/article/details/4226630
the code knocks less Ah, even scanf () grammar also card good assembly son. But I know it later ...
I actually feel that string processing strings is also very handy.
#include <iostream > #include <stdio.h> #include <cstring>const int m=500;using namespace Std;int main ( ) { char cnt[m],dic[m]; int c[m][m],k,m; while (scanf ("%s%s", cnt+1,dic+1)!=eof) { int lencnt=strlen (cnt+1); int Lendic=strlen (dic+1); for (int i=1; i<=lencnt; i++) c[i][0]=0; for (int j=0; j<=lendic; j + +) c[0][j]=0; For (k=1, k<=lencnt; k++) for (m=1; m<=lendic; m++) { if (Cnt[k]==dic[m]) { c[k][m]=c[ k-1][m-1]+1; } else { C[k][m]=max (c[k-1][m],c[k][m-1]); } } cout<<c[lencnt][lendic]<<endl; } return 0;}
Hangzhou Electric HDU ACM 1159 Common subsequence