poj 1458 LCS

经典dp问题:求两个字符串的LCS(最长公共子序列)。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 const int N = 1001;
 7 char a[N], b[N];
 8 int dp[N][N];
 9 
10 int main ()
11 {
12     while ( scanf("%s%s", a, b) != EOF )
13     {
14         int la = strlen(a), lb = strlen(b);
15         memset( dp, 0, sizeof(dp) );
16         for ( int i = 1; i <= la; i++ )
17         {
18             for ( int j = 1; j <= lb; j++ )
19             {
20                 if( a[i - 1] == b[j - 1] )
21                 {
22                     dp[i][j] = dp[i - 1][j - 1] + 1;
23                 }
24                 else
25                 {
26                     dp[i][j] = max( dp[i][j - 1], dp[i - 1][j] );
27                 }
28             }
29         }
30         printf("%d
", dp[la][lb]);
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/huoxiayu/p/4644729.html