LCS,动态规划基本题型...
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1159
代码如下:
#include <stdio.h>
#include <string.h>
char a[1000005], b[1000005];
int dp[10005][10005];
int lena, lenb;
int max( int x, int y )
{
return x>y ? x : y;
}
int LCS( int lena, int lenb )
{
int len = max(lena, lenb);
for( int i = 0; i <= len; i++ )
{
dp[i][0] = 0;
}
for( int i = 1; i <= lena; i++ )
for( int j = 1; j <= lenb; j++ )
{
if(a[i-1] == b[j-1])
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
return dp[lena][lenb];
}
int main()
{
while(scanf("%s %s",a,b) == 2 )
{
lena = strlen(a);
lenb = strlen(b);
printf("%d\n",LCS(lena,lenb));
}
return 0;
}