首页 > 其他 > 详细

HDOJ 4632 Palindrome subsequence

时间:2014-03-30 02:20:25      阅读:532      评论:0      收藏:0      [点我收藏+]

区间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 = <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
 


#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,布布扣,bubuko.com

HDOJ 4632 Palindrome subsequence

原文:http://blog.csdn.net/u012797220/article/details/22541495

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!