Common subsequenceTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 24489 Accepted Submission (s): 10823
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
#include <stdio.h> #include <string> #include <vector> #include <iostream>using namespace std; int main (int argc, char *argv[]) {string A, B; while (cin>>a>>b) {vector<vector<int> > C; int x=a.size (); int y=b.size (); int size=x>y?x:y; Size+=1; C.resize (SIZE); for (int i=0;i<size;++i) c[i].resize (size,0); for (int i=0;i<=x;++i) c[i][0]=0; for (int i=0;i<=y;++i) c[0][i]=0; for (int i=1;i<=x;++i) for (int j=1;j<=y;++j) {if (a[i-1]==b[j-1]) {c[i][j]=c[i-1][j-1]+1; } else {c[i][j]=c[i][j-1]>c[i-1][j]?c[i][j-1]:c[i-1][j]; }} cout<<c[x][y]<<endl; } return 0;}
Hdu 1159 Common Subsequence (DP seeking LCS)