Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23973 Accepted Submission(s): 10592
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define max(a,b) a>b?a:b #define INF 0x7ffffff #define N 1010 int dp[N][N]; //dp[i][j]表示a中前i个字符和b中前j个字符的最长公共子序列 char s1[N],s2[N]; int main() { int i,j; while(scanf("%s%s",s1+1,s2+1)!=EOF) { int l1=strlen(s1+1); int l2=strlen(s2+1); memset(dp,0,sizeof(dp)); for(i=1;i<=l1;i++) { for(j=1;j<=l2;j++) { if(s1[i]==s2[j]) { dp[i][j]=dp[i-1][j-1]+1; } else { dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } } cout<<dp[l1][l2]<<endl; } return 0; }
最长公共子序列 [HDU 1159] Common Subsequence
原文:http://www.cnblogs.com/hate13/p/4050418.html