您的位置:首页 > 产品设计 > UI/UE

poj 1458 Common Subsequence(lcs)

2017-02-15 16:35 357 查看

题目链接:

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

题意:

求两个字符串的lcs

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 1000;

int dp[maxn][maxn];
char s[maxn];
char s1[maxn],s2[maxn];

int main()
{
while(gets(s))
{
sscanf(s,"%s %s",s1,s2);
int len1 = strlen(s1),len2 = strlen(s2);
memset(dp,0,sizeof(dp));
for(int i = 1 ; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1])
{
dp[i][j] = dp[i-1][j-1] + 1;
}
else
{
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
printf("%d\n",dp[len1][len2]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 动态规划