现在的位置: 首页 > 综合 > 正文

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

2019年02月21日 ⁄ 综合 ⁄ 共 1367字 ⁄ 字号 评论关闭
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]);
    }
}

抱歉!评论已关闭.