1025

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


PROBLEM SETTER: MUNTASIR MUZAHID CHOWDHURY
SPECIAL THANKS: JANE ALAM JAN (DATASET)
题意:给你一字符串,问你在这字符串里去掉一些字符构成回文串,有多少种方法;
思路:区间dp;
dp[i][j]表示在区间i,j中有多少个回文,然后状态转移方程dp[i][j]=dp[i][j-1]+dp[i+1][j];这里面有重复的这个时候分情况讨论,ans[i]==ans[j]的时候
我们有dp[i][j]=dp[i][j-1]+dp[i+1][j]+1,中间回文和两端构成新的回文,那么中间也就不需要删除了,并且加了问空时,就两端构成回文,当不等时
dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<stdlib.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<stack>
 9 #include<vector>
10 using namespace std;
11 typedef long long LL;
12 LL dp[100][100];
13 char ans[100];
14 int main(void)
15 {
16         int i,j,k;
17         int s;
18         scanf("%d",&k);
19         for(s=1; s<=k; s++)
20         {
21                 scanf("%s",ans);
22                 int l=strlen(ans);
23                 memset(dp,0,sizeof(dp));
24                 for(i=0; i<l; i++)
25                 {
26                         for(j=i; j>=0; j--)
27                         {
28                                 if(i==j)dp[j][i]=1;
29                                 else
30                                 {
31                                         if(ans[i]==ans[j])
32                                         {
33                                          dp[j][i]=dp[j][i-1]+dp[j+1][i]+1;
34                                         }
35                                         else
36                                         {
37                                         dp[j][i]= dp[j][i]=dp[j][i-1]+dp[j+1][i]-dp[j+1][i-1];
38                                         }
39                                 }
40                         }
41                 }
42                 printf("Case %d: %lld
",s,dp[0][l-1]);
43         }
44         return 0;
45 }
油!油!you@
原文地址:https://www.cnblogs.com/zzuli2sjy/p/5602452.html