首页 > 其他 > 详细

Light OJ 1025 - The Specials Menu(区间DP)

时间:2015-10-25 10:44:26      阅读:259      评论:0      收藏:0      [点我收藏+]

题目大意:

    给你一个字符串,问有多少种方法删除字符,使得剩下的字符是回文串。
有几个规定:
1.空串不是回文串
2.剩下的字符位置不同也被视为不同的回文串。如:AA有三种回文串 A, A, AA
================================================================================================
 
#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;
char str[MAXN];
LL dp[MAXN][MAXN];
LL DFS(int L,int R)
{
    if(L > R) return 0;
    if(dp[L][R]) return dp[L][R];
    if(L == R) return dp[L][R] = 1;

    dp[L][R] = DFS(L+1,R) + DFS(L,R-1) - DFS(L+1,R-1);
    if(str[L] == str[R])
        dp[L][R] += DFS(L+1, R-1) + 1;
    return dp[L][R];
}

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

    return 0;
}

 

Light OJ 1025 - The Specials Menu(区间DP)

原文:http://www.cnblogs.com/chenchengxun/p/4908262.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!