Description
Input
Output
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
#include<algorithm> #include<stdio.h> #include<string.h> using namespace std; char a[2010]; char b[2010]; int dp[2010][2010]; int main(){ while(scanf("%s%s",&a,&b)!=EOF){ int a1=strlen(a); int b1=strlen(b); memset(dp,0,sizeof(dp)); for(int i=1;i<a1+1;i++) { for(int j=1;j<b1+1;j++) { if(a[i-1]==b[j-1]) { dp[i][j]=dp[i-1][j-1]+1; } else { if(dp[i-1][j]>dp[i][j-1]) { dp[i][j]=dp[i-1][j]; } else { dp[i][j]=dp[i][j-1]; } } } } printf("%d\n",dp[a1][b1]); } return 0; }
SDJZU_新生_递推动规_HDU 1159 Common Subsequence
原文地址:http://blog.csdn.net/floating_life/article/details/43267255