标签:
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 starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains a single word W (1 ≤ length(W) ≤ 60).
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 |
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\n",s,dp[0][l-1]); 43 } 44 return 0; 45 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5602452.html