首页 > 其他 > 详细

hdu4632Palindrome subsequence (求回文数,区间DP)

时间:2014-07-21 11:13:44      阅读:246      评论:0      收藏:0      [点我收藏+]
Problem Description

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
#include<stdio.h>
#include<string.h>
int t,dp[1005][1005],c=0;
int main()
{
    char str[1005];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int len=strlen(str);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<len;i++)
        dp[i][i]=1;
        for(int r=1;r<len;r++)
        for(int i=0;i<len-r;i++)
        {
            int j=i+r;
            dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007;
            if(str[i]==str[j])
            dp[i][j]=(dp[i][j]+dp[i+1][j-1]+1)%10007;
        }
        printf("Case %d: %d\n",++c,dp[0][len-1]);
    }
}


hdu4632Palindrome subsequence (求回文数,区间DP)

原文:http://blog.csdn.net/u010372095/article/details/38010169

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