uva 10405 Longest Common Subsequence
就是一个裸的LCS,不过注意用gets读入
#include <cstdio> #include <cstring> #define N 1010 int main() { int n,m,dp[N][N]; char s1[N],s2[N]; while(gets(s1+1) && gets(s2+1)) { memset(dp,0,sizeof(dp)); n=strlen(s1+1); m=strlen(s2+1); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=dp[i-1][j]>dp[i][j-1]?dp[i-1][j]:dp[i][j-1]; printf("%d\n",dp[n][m]); } return 0; }