Live2d Test Env

HihoCOder1323 : 回文字符串(区间DP)

 回文字符串

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

给定一个字符串 S ,最少需要几次增删改操作可以把 S 变成一个回文字符串?

一次操作可以在任意位置插入一个字符,或者删除任意一个字符,或者把任意一个字符修改成任意其他字符。

输入

字符串 SS 的长度不超过100, 只包含'A'-'Z'。

输出

最少的修改次数。

样例输入
ABAD
样例输出
1

区间DP水题,见铺垫:密码脱落

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
char c[110];
int dp[110][110];
int main()
{
    int i,j,Len;
    scanf("%s",c+1);
    Len=strlen(c+1);
    for(i=Len-1;i>=1;i--){
        for(j=i;j<=Len;j++){
           dp[i][j]=dp[i+1][j]+1;
           dp[i][j]=min(dp[i][j-1]+1,dp[i][j]);
           if(c[i]==c[j]) dp[i][j]=min(dp[i][j],dp[i+1][j-1]); 
           else dp[i][j]=min(dp[i][j],dp[i+1][j-1]+1);
        }
    }
    printf("%d\n",dp[1][Len]);
    return 0;
}

 

posted @ 2017-11-12 13:57  nimphy  阅读(301)  评论(0编辑  收藏  举报