题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092
回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。每个字符串都可以通过向中间添加一些字符,使之变为回文字符串。
例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba。方案1只需要添加2个字符,是所有方案中添加字符数量最少的。
Input
输入一个字符串Str,Str的长度 <= 1000。
Output
输出最少添加多少个字符可以使之变为回文字串。
分析:将给定字符串,反串。然后跟给定串求LCS(最长公共子序列):。然后总的字符串长度减去它,就是答案
<span style="font-size:24px;">#include"stdlib.h"
#include"stdio.h"
#include"string.h"
#include"algorithm"
#include"iostream"
#include"math.h"
using namespace std;
const int maxn=1004;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
//LCS问题
int main()
{
scanf("%s",s1);
int len=strlen(s1);
for(int i=0;i<len;i++)
s2[len-1-i]=s1[i];
memset(dp,0,sizeof(dp));
for(int i=1;i<=len;i++)
{
for(int j=1;j<=len;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 ",dp[i][j]);
}
// printf("\n");
}
printf("%d\n",len-dp[len][len]);
return 0;
} </span>