hdu 1159

最长公共子串

 1 #include <stdio.h>
 2 #include<string.h>
 3 char x[2000],y[2000];
 4 int dp[2001][2001];
 5 int main(int argc, char *argv[])
 6 {
 7     while (scanf("%s %s",x,y)!=EOF)
 8     {
 9         int len1=strlen(x);
10         int len2=strlen(y);
11         int i,j;
12         memset(dp,0,sizeof(dp));
13         for(i=1;i<=len1;i++)
14         {
15             for(j=1;j<=len2;j++)
16             {
17                 if(x[i-1]==y[j-1])
18                 {
19                     dp[i][j]=dp[i-1][j-1]+1;
20                 }
21                 else
22                 {
23                     dp[i][j]=dp[i-1][j]>dp[i][j-1]?dp[i-1][j]:dp[i][j-1];
24                 }
25             }
26         }
27         printf("%d\n",dp[len1][len2]);
28     }
29     return 0;
30 }
posted @ 2012-11-28 20:38  zerojetlag  阅读(164)  评论(0编辑  收藏  举报