poj 1458 Common Subsequence ——(LCS)

  虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧。

  直接贴代码就好了吧:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 const int N = 1000 + 5;
 6 
 7 char a[N],b[N];
 8 int dp[N][N];
 9 
10 int main()
11 {
12     while(scanf("%s%s",a+1,b+1) == 2)
13     {
14         int n = strlen(a+1);
15         int m = strlen(b+1);
16         memset(dp,0,sizeof dp);
17         for(int i=1;i<=n;i++)
18         {
19             for(int j=1;j<=m;j++)
20             {
21                 if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1] + 1;
22                 else dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
23             }
24         }
25         printf("%d
",dp[n][m]);
26     }
27 }
原文地址:https://www.cnblogs.com/zzyDS/p/6400516.html