51nod 1154【DP】

区间DP大暴力吧?GG.

dp[ i ] 为字符至 i 的最少数量。

如果[Left , Right]是回文串,

dp[Right] = min(dp[ Right ] , dp[Left-1] + 1);

#include<bits/stdc++.h>
using namespace std;

const int N=5e3+10;

int dp[N];
char a[N];
int n;

int main(){
    scanf("%s",a+1);n=strlen(a+1);

    for(int i=0;i<=n;i++)
        dp[i]=i;

    for(int i=1;i<=n;i++)
    {
        int Left,Right;
        //aba
        Left=Right=i;
        while(Left>=1&&Right<=n&&a[Left]==a[Right])
        {
            dp[Right]=min(dp[Right],dp[Left-1]+1);
            Right++;
            Left--;
        }
        //abba
        Left=i;Right=i+1;
        while(Left>=1&&Right<=n&&a[Left]==a[Right])
        {
            dp[Right]=min(dp[Right],dp[Left-1]+1);
            Right++;
            Left--;
        }
       // printf("%d\n",dp[i]);
    }
    printf("%d\n",dp[n]);
    return 0;
}



posted @ 2017-03-28 19:37  see_you_later  阅读(221)  评论(0编辑  收藏  举报