最长公共子序列POJ1458

http://poj.org/problem?id=1458

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 char a[1005],b[1005];
 6 int dp[1005][1005];
 7 main()
 8 {
 9     int i,j,Q,W;
10     while(scanf("%s%s",a+1,b+1)!=EOF)
11     {
12         Q=strlen(a+1);
13         W=strlen(b+1);
14         memset(dp,0,sizeof(dp));
15         for(i=1;i<=strlen(a+1);i++)
16         {
17             for(j=1;j<=strlen(b+1);j++)
18             {
19                 if(a[i]==b[j])
20                 {
21                     dp[i][j]=dp[i-1][j-1]+1;
22                 }
23                 else
24                 {
25                     dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
26                 }
27             }
28         }
29         printf("%d\n",dp[Q][W]);
30     }
31 }
View Code

 

posted @ 2016-07-21 15:24  Crazy、baby  阅读(220)  评论(0编辑  收藏  举报