Common Subsequence

 1 /*HDU1159 ,最长公共*/
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 int dp[1000][1000];
 7 char a[1000],b[1000];
 8 int LCS(int n,int m)
 9 {
10     int i,j;
11     for(i=1;i<=n;i++)
12         for(j=1;j<=m;j++)
13         {
14             if(a[i-1]==b[j-1])
15             {
16                 dp[i][j]=dp[i-1][j-1]+1;
17             }
18             else if(dp[i-1][j]>=dp[i][j-1])
19             {
20                 dp[i][j]=dp[i-1][j];
21 
22             }
23             else
24             {
25                 dp[i][j]=dp[i][j-1];
26             }
27         }
28     return dp[n][m];
29 }
30 int main()
31 {
32     int n,m;
33     while(scanf("%s%s",a,b)!=EOF)
34     {
35         n=strlen(a);
36         m=strlen(b);
37         printf("%d\n",LCS(n,m));
38     }
39      return 0;
40 }

 

posted on 2013-08-05 16:06  ok_boy  阅读(194)  评论(0编辑  收藏  举报

导航