标签:
1 #include <iostream> 2 #include <string> 3 4 //求最长公共子序列 5 using namespace std; 6 7 int const MAX_NUM = 1010; 8 int dp[MAX_NUM][MAX_NUM]; 9 10 int max(int a,int b){ 11 return a>b?a:b; 12 } 13 14 int main(){ 15 16 string s1,s2; 17 while(cin>>s1>>s2){ 18 int len1 = s1.length(); 19 int len2 = s2.length(); 20 21 for(int i = 1; i <= len1; i++){ 22 for(int j = 1; j <= len2; j++){ 23 dp[i][j] = 0; 24 } 25 } 26 27 for(int i = 1; i <= len1; i++){ 28 for(int j = 1; j <= len2; j++){ 29 if(s1[i-1] == s2[j-1]){ 30 dp[i][j] = dp[i-1][j-1]+1; 31 }else{ 32 dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 33 } 34 } 35 } 36 37 cout<<dp[len1][len2]<<endl; 38 } 39 40 return 0; 41 }
标签:
原文地址:http://www.cnblogs.com/hu983/p/5577005.html