HDU 1159 Common Subsequence
最长公共子序列
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=1111; int dp[maxn][maxn]; char s[maxn],t[maxn]; int lens,lent; int main() { int i,j; while(~scanf("%s%s",s,t)) { lens=strlen(s); lent=strlen(t); for(i=-1; i<lens; i++) for(j=-1; j<lent; j++) { if(i==-1||j==-1) { dp[i+1][j+1]=0; continue; } if(s[i]==t[j]) dp[i+1][j+1]=dp[i][j]+1; else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]); } printf("%d\n",dp[lens][lent]); } return 0; }