51nod 1092 回文字符串

基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。每个字符串都可以通过向中间添加一些字符,使之变为回文字符串。
例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba。方案1只需要添加2个字符,是所有方案中添加字符数量最少的。
 
Input
输入一个字符串Str,Str的长度 <= 1000。
Output
输出最少添加多少个字符可以使之变为回文字串。
Input示例
abbc
Output示例
2


反转字符串
答案=原长-lcs
屠龙宝刀点击就送

#include <cstring>
#include <cstdio>
int dp[1005][1005];
char str1[1005],str2[1005];
int max(int a,int b){return a>b?a:b;} 
int main()
{
    scanf("%s",str1);
    int l1=strlen(str1);
    for(int i=l1-1;i>=0;i--) str2[l1-i-1]=str1[i];
    int l2=strlen(str2);
    for(int i=1;i<=l1;i++)
    {
        for(int j=1;j<=l2;j++)
        if(str1[i-1]==str2[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",l1-dp[l1][l2]);
    return 0;
}



我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
原文地址:https://www.cnblogs.com/ruojisun/p/6724428.html