您的位置:首页 > 其它

51Nod 1092 回文字符串 | 最长公共子序列变形

2017-09-05 16:02 645 查看

 

 

求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长

#include "stdio.h"
#include "string.h"
#define maxn 1005
char s[maxn],s1[maxn];
int dp[maxn][maxn];
int main()
{
int n=0,i,j,len;
scanf("%s",s);
len=strlen(s);
strcpy(s1,s);
strrev(s1);
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
if(s[i]==s1[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=dp[i+1][j]>dp[i][j+1]?dp[i+1][j]:dp[i][j+1];
}
}
printf("%d\n",len-dp[len][len]);
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: