Light OJ 1025 The Specials Menu 详细题解(回文串的区间DP)

16 篇文章 3 订阅


1025 - The Specials Menu
Time Limit: 2 second(s)Memory Limit: 32 MB

Feuzem is an unemployed computer scientist who spends his days working at odd-jobs. While on the job he always manages to find algorithmic problems within mundane aspects of everyday life.

Today, while writing down the specials menu at the restaurant he's working at, he felt irritated by the lack of palindromes (strings which stay the same when reversed) on the menu. Feuzem is a big fan of palindromic problems, and started thinking about the number of ways he could remove letters from a particular word so that it would become a palindrome.

Two ways that differ due to order of removing letters are considered the same. And it can also be the case that no letters have to be removed to form a palindrome.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a single word W (1 ≤ length(W) ≤ 60).

Output

For each case, print the case number and the total number of ways to remove letters from W such that it becomes a palindrome.

Sample Input

Output for Sample Input

3

SALADS

PASTA

YUMMY

Case 1: 15

Case 2: 8

Case 3: 11



题意:对于字符串S,可以从S中去掉任意字符,形成的字符串中有多少是回文串。

定义dp[i][j] 为区间i ~ j 中有多少回文串, 当(i == j)时,dp[i][j] = 1。

1)s[i] != s[j] , 那么dp[i][j]只能由 dp[i+1][j] 和 dp[i][j-1] 转移过来,而且要减去

重复区间dp[i+1][j-1]。

2)s[i] == s[j], dp[i][j] 除了由 (1)状态贡献之外,还要加上dp[i+1][j-1]贡献的。


还是要深刻理解区间DP的!!! 首先这题,不能用贪心,不能用暴力,线性DP也想不出怎么转移,就是区间DP没跑了。。。 区间DP,是由大区间都是由小区间转移而来的,  这里要明白,一个区间为5的dp,他是由区间为4的转移过来的,这时候想怎么转移。。。然后很简单就想到当s[i] != s[j]时,dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];当s[i] == s[j]时,其实就是左面区间n-1,根右面区间长度是n-1的回文串+ 整个区间n的回文串(其实就是dp[i+1][j-1])+1(s[i] 跟 s[j])。。。一开始想怎么处理扣掉的字符,是不是还要写一个函数判断回文。。。DP,DP,核心就是状态转移啊、、、这个也只有区间DP能做了,自然想区间dp性质,枚举长度,枚举起点,这样某一长度所有状态都就知道了,大的状态都是由小的状态来的。。


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100;
char s[maxn];
long long dp[maxn][maxn];
int main()
{
    int t, ca = 1;
    cin >> t;
    while(t--)
    {
        memset(dp, 0, sizeof(dp));
        cin >> s;
        int n = strlen(s);
        for(int i = 0; i < n; i++)
            dp[i][i] = 1;
        for(int len = 1; len < n; len++)
        {
            for(int i = 0; i+len < n; i++)
            {
                int j = i + len;
                if(s[i] == s[j])
                    dp[i][j] = dp[i+1][j-1] + 1; // dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1] + (dp[i+1][j-1]+1);
                else
                    dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];
            }
        }
        printf("Case %d: %lld\n", ca++, dp[0][n-1]);
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值