首页 > 代码库 > HDU 1159 LCS最长公共子序列

HDU 1159 LCS最长公共子序列

 1 #include <cstdio> 2 #include <cstring> 3  4 using namespace std; 5 const int N = 1005; 6 #define max(a,b) a>b?a:b 7  8 char a[N] , b[N]; 9 int dp[N][N];10 11 int main()12 {13     while(scanf("%s%s" , a+1 , b+1) != EOF){14         int l1 = strlen(a+1);15         int l2 = strlen(b+1);16         for(int i = 1 ; i<=l1 ; i++)17             for(int j = 1 ; j<=l2 ; j++){18                 if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1]+1;19                 else dp[i][j] = max(dp[i-1][j] , dp[i][j-1]);20             }21         printf("%d\n" , dp[l1][l2]);22     }23     return 0;24 }

 

HDU 1159 LCS最长公共子序列