HDU 1159 Common Subsequence

  
    
/*
经典的动态规划
*/
#include
< iostream >
using namespace std;
char str1[ 1000 ];
char str2[ 1000 ];
int dp[ 1001 ][ 1001 ];
int main(){
while (cin >> str1 >> str2){
int len1 = strlen(str1);
int len2 = strlen(str2);
int max = - 1 ;
memset(dp,
0 , sizeof (dp));
for ( int i = 0 ; i < len1; i ++ ){
for ( int j = 0 ; j < len2; j ++ ){
if (str1[i] == str2[j]){
dp[ i
+ 1 ][j + 1 ] = dp[i][j] + 1 ;
}
else {
dp[i
+ 1 ][j + 1 ] = dp[i][j + 1 ] >
dp[i
+ 1 ][j] ? dp[i][j + 1 ] :
dp[i
+ 1 ][j];
}
if (max < dp[i + 1 ][j + 1 ])
max
= dp[i + 1 ][j + 1 ];
}
}
cout
<< max << endl;
}
return 0 ;
}

你可能感兴趣的:(sequence)