HDOJ 4632 Palindrome subsequence

区间DP。。。。

Palindrome subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others)
Total Submission(s): 2021    Accepted Submission(s): 836


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 = <S x1, S x2, ..., S xk> and Y = <S y1, S y2, ..., S yk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S xi = S yi. 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
 


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int MOD=10007;
const int maxn=1100;

int dp[maxn][maxn],n;
char str[maxn];

int main()
{
    int T_T,cas=1;
    scanf("%d",&T_T);
while(T_T--)
{
    scanf("%s",str);
    n=strlen(str);
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++) dp[i][i]=1;

    for(int i=2;i<=n;i++)
    {
        for(int j=0;j+i-1<n;j++)
        {
            int from=j,to=j+i-1;

            if(str[from]==str[to])
            {
                dp[from][to]++;
            }
            else
            {
                dp[from][to]-=dp[from+1][to-1];
            }
            dp[from][to]+=dp[from+1][to]+dp[from][to-1];
            dp[from][to]%=MOD;
        }
    }

    printf("Case %d: %d\n",cas++,(dp[0][n-1]+MOD)%MOD);
}
    return 0;
}




你可能感兴趣的:(HDOJ 4632 Palindrome subsequence)