The Specials Menu LightOJ - 1025

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

3

SALADS

PASTA

YUMMY

Sample Output

Case 1: 15

Case 2: 8

Case 3: 11

注意:答案可能超过long long

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 
 8 ll n,dp[105][105];
 9 string S;
10 
11 void solve(int t){
12     memset(dp,0,sizeof(dp));
13     for(int i=n-1;i>=0;i--){
14         for(int j=i;j<n;j++){
15             dp[i][j]+=dp[i][j-1];
16             dp[i][j]+=dp[i+1][j];
17             if(S[i]==S[j]) dp[i][j]+=1;
18             else dp[i][j]-=dp[i+1][j-1];
19         }
20     }
21     printf("Case %d: %lld\n",t,dp[0][n-1]);
22 }
23 
24 int main()
25 {   int kase;
26     cin>>kase;
27     for(int t=1;t<=kase;t++){
28         cin>>S;
29         n=S.size();
30         solve(t);
31     }
32     return 0;
33 }

 

posted @ 2017-09-07 14:37  天之道,利而不害  阅读(190)  评论(0编辑  收藏  举报