码迷,mamicode.com
首页 > 其他好文 > 详细

Light OJ 1033 - Generating Palindromes(区间DP)

时间:2015-10-26 20:46:10      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:
给你一个字符串,问最少增加几个字符使得这个字符串变为回文串。
 
=======================================================================================
#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;
}

 

Light OJ 1033 - Generating Palindromes(区间DP)

标签:

原文地址:http://www.cnblogs.com/chenchengxun/p/4912221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!