Light OJ 1033 - Generating Palindromes(区间DP)

题目大意:
给你一个字符串,问最少增加几个字符使得这个字符串变为回文串。
 
=======================================================================================
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9+7;
const int MAXN = 255;
int dp[MAXN][MAXN];
char str[MAXN];
int DFS(int L,int R)
{
    if(dp[L][R] != -1) return dp[L][R];
    if(L >= R) return dp[L][R] = 0;

    dp[L][R] = INF;
    for(int i=L; i<=R; i++)
    {
        if(str[L] == str[i])
            dp[L][R] = min(dp[L][R], R - i + DFS(L+1,i-1));
        dp[L][R] = min(dp[L][R], DFS(L+1,i)+R-i+1);
    }
    return dp[L][R];
}

int main()
{
    int T, cas = 1, n;

    scanf("%d", &T);
    while(T --)
    {
        memset(dp, -1, sizeof(dp));
        scanf("%s", str);
        printf("Case %d: %d\n",cas++, DFS(0, strlen(str)-1));
    }

    return 0;
}

 

posted @ 2015-10-26 20:03  向前走丶不回首  阅读(112)  评论(0编辑  收藏  举报