题目链接:
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;
}

本文提供了一个解决POJ 1458问题的C++代码示例,该问题要求找出两个字符串的最长公共子序列(LCS)。通过使用动态规划的方法,程序有效地解决了这一挑战。
1171

被折叠的 条评论
为什么被折叠?



