hdu 4632 Palindrome subsequence

Palindrome subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/Others)
Total Submission(s): 1836 Accepted Submission(s): 770


Problem Description
In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence <A, B, D> is a subsequence of <A, B, C, D, E, F>.
(http://en.wikipedia.org/wiki/Subsequence)

Given a string S, your task is to find out how many different subsequence of S is palindrome. Note that for any two subsequence X = <Sx1, Sx2, ..., Sxk> and Y = <Sy1, Sy2, ..., Syk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if Sxi = Syi. Also two subsequences with different length should be considered different.


Input
The first line contains only one integer T (T<=50), which is the number of test cases. Each test case contains a string S, the length of S is not greater than 1000 and only contains lowercase letters.


Output
For each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.


Sample Input
4
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems


Sample Output
Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960


Source
2013 Multi-University Training Contest 4


Recommend
zhuyuanchen520

 

 

 1 //343MS    4176K    673 B    C++
 2 /*
 3 
 4     题意:
 5         求字符串有多少个子序列串是回文串
 6     
 7     区间DP:
 8         dp[i][j]表示i~j间字符串拥有的回文串子串
 9         
10         状态转移:
11              dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1];
12              if(c[i]==c[j]) 
13                  dp[i][j]+=dp[i+1][j-1]+1; 
14 
15 */
16 #include<stdio.h>
17 #include<string.h>
18 #define N 1005
19 #define M 10007
20 int dp[N][N];
21 char c[N];
22 int main(void)
23 {
24     int t,n;
25     int k=1;
26     scanf("%d",&t);
27     while(t--)
28     {
29         memset(dp,0,sizeof(dp));
30         scanf("%s",c);
31         int n=strlen(c);
32         for(int i=0;i<n;i++)
33             dp[i][i]=1;
34         for(int j=0;j<n;j++){  //前 j 个字符 
35             for(int i=j-1;i>=0;i--){ //i~j 间的字符符合的数 
36                 dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+M;
37                 if(c[i]==c[j]) 
38                     dp[i][j]+=dp[i+1][j-1]+1;
39                 dp[i][j]%=M;
40                 //printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
41             } 
42         }
43         printf("Case %d: %d\n",k++,dp[0][n-1]);
44     }
45     return 0;
46 } 

 

posted @ 2013-10-12 22:29  heaventouch  阅读(121)  评论(0编辑  收藏  举报