http://acm.hdu.edu.cn/showproblem.php?pid=1159
最长公共子序列不解释
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
char word1[1005];
char word2[1005];
int dp[1005][1005];
int main()
{
while(scanf("%s%s",word1+1,word2+1)==2)
{
memset(dp,0,sizeof(dp));
int len1=strlen(word1+1);
int len2=strlen(word2+1);
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++)
{
if(word1[i]==word2[j])
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]);
}
}

本文介绍了一个经典的动态规划问题——最长公共子序列问题,并提供了一段完整的C++代码实现。该算法通过二维动态规划数组记录两个字符串之间的最长公共子序列长度。
456

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



