SDUT 2080 最长公共子序列问题

很水很水的DP。课本上的模板题。

题目链接http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2080

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 int map[510][510];
 4 char str1[510],str2[510];
 5 int main()
 6 {
 7     int len1,len2,i,j;
 8     while(gets(str1))
 9     {
10         gets(str2);
11         len1=strlen(str1);
12     len2=strlen(str2);
13     memset(map,0,sizeof(map));
14     for(i=1;i<=len1;i++)
15     {
16         for(j=1;j<=len2;j++)
17         if(str1[i-1]==str2[j-1])//注意这里是str1[i-1]==str2[j-1]
18     {
19         map[i][j]=map[i-1][j-1]+1;
20     }
21     else
22     {
23         if(map[i-1][j]>map[i][j-1])
24         map[i][j]=map[i-1][j];
25         else map[i][j]=map[i][j-1];
26     }
27     }
28     printf("%d\n",map[len1][len2]);
29     }
30     return 0;
31 }
posted @ 2012-08-15 21:00  时光旅行的懒猫  阅读(296)  评论(0编辑  收藏  举报