poj1458

 1 /* 最长公共子序列*/
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 int dp[1000][1000],dir[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                 dir[i][j]=1;
18             }
19             else if(dp[i-1][j]>=dp[i][j-1])
20             {
21                 dp[i][j]=dp[i-1][j];
22                 dir[i][j]=0;
23             }
24             else
25             {
26                 dp[i][j]=dp[i][j-1];
27                 dir[i][j]=2;
28             }
29         }
30     return dp[n][m];
31 }
32 /*void print(int r,int c)
33 {
34      if(r==0 || c==0) return;
35      if(dir[r][c]==1)
36      {
37          print(r-1,c-1);
38          printf("%c ",a[r-1]);
39      }
40      else if(dir[r][c]==0) print(r-1,c);
41      else print(r,c-1);
42 }*/
43 int main()
44 {
45     int n,m;
46     while(scanf("%s%s",a,b)!=EOF)
47     {
48         //printf("%s\n%s\n",a,b);
49         n=strlen(a);
50         m=strlen(b);
51         printf("%d\n",LCS(n,m));
52         //print(n,m);
53     //    printf("\n");
54     }
55     return 0;
56 }
57         

 

posted on 2013-07-29 18:11  ok_boy  阅读(194)  评论(0编辑  收藏  举报

导航