POJ1458 Common Subsequence(最长公共子序列)
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <cstdio> using namespace std; int cmp[1010][1010]; char s1[1010],s2[1010]; int main() { //freopen("in.txt","r",stdin); while(~scanf("%s%s",s1+1,s2+1)) { int l1=strlen(s1+1),l2=strlen(s2+1); for(int i=0;i<=l1;i++) cmp[i][0]=0; for(int i=0;i<=l2;i++) cmp[0][l2]=0; for(int i=1;i<=l1;i++) { for(int j=1;j<=l2;j++) { if(s1[i]==s2[j]) cmp[i][j]=cmp[i-1][j-1]+1; else cmp[i][j]=max(cmp[i-1][j],cmp[i][j-1]); } } printf("%d\n",cmp[l1][l2]); } return 0; }