hdu4632 Palindrome subsequence

Palindrome subsequence

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

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

dp[i][j] 表示 包含 a[i]的,到 j 的回文个数

转移:

dp[i][j] = 1 ;

dp[i][j] = dp[i][j-1] ; i < j

if(a[i]==a[j]) dp[i][j] += dp[k][j-1]  ; i < k < j

这样是n^3的,会T

所以用 sum[i][j] 表示到回文个数,优化转移

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define maxn 100010
#define LL long long
#define mod 10007
using namespace std;

int dp[1010][1010],sum[1010][1010];
char a[1010] ;
int main()
{
    int n,i,j,m;
    int T,case1=0;
    cin >> T ;
    while( T-- )
    {
        scanf("%s",a+1) ;
        n = strlen(a+1) ;
        memset(dp,0,sizeof(dp)) ;
        memset(sum,0,sizeof(sum)) ;
        for( i = n ; i >= 1 ;i--)
            for( j = i ; j <= n ;j++)
        {
            if(i==j)
            {
                dp[i][j]=1;
                sum[i][j]=1;
                continue;
            }
            dp[i][j] = dp[i][j-1] ;
            if(a[i]==a[j])
            {
                dp[i][j]++;
               // for(int k = i+1 ; k < j ;k++)
                   // dp[i][j] += dp[k][j-1];
                   dp[i][j] += sum[i+1][j-1] ;
            }
            dp[i][j] %= mod;
            sum[i][j] = (sum[i+1][j]+dp[i][j])%mod;
        }
        int ans=0;
        for(i = 1 ; i <= n ;i++)
            ans = (ans+dp[i][n])%mod ;
        printf("Case %d: %d\n",++case1,ans);
    }
    return 0;
}

  

hdu4632 Palindrome subsequence,布布扣,bubuko.com

时间: 2024-08-28 04:29:59

hdu4632 Palindrome subsequence的相关文章

hdu4632 Palindrome subsequence 回文子序列个数 区间dp

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others)Total Submission(s): 4513    Accepted Submission(s): 1935 Problem Description In mathematics, a subsequence is a sequence that can be derived f

hdu-4632 Palindrome subsequence (回文子序列计数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 问题要求回答一串字符串中回文子序列的数量,例如acbca就有 a,c,b,c,a,cc,aa,aca,aca(注意这两个aca的c是不同位置的c,都要累计),aba,cbc,acca,acbca.共13种. 我们如果构造dp[i][j]为区间从i-j的回文子序列个数,当i==j时dp[i][j]=1,当i!=j时,如果字符串i,j位相等,他们便可以从dp[i+1,j-1]转移而来,即dp[i]

HDU4632 Palindrome subsequence 题解 区间DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632[题目描述]<回文子串数量>给你一个长度为N(N≤1000)的字符串,你输出它所有的回文子串的数量(对10007取模).只要从字符串 s 中顺序地取出一些字符(不要求连续,可以是 s 本身,不能是空串),不改变他们的顺序的情况下,这个子串是一个回文串,那么他就是一个 s 的回文子串.[输入格式]首先是一个数T(T≤50),表示测试用例的数量.接下来T行,每行包含一个字符串.[输出格式]对于每一

HDU 4632 Palindrome subsequence (区间dp 容斥定理)

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others) Total Submission(s): 2610    Accepted Submission(s): 1050 Problem Description In mathematics, a subsequence is a sequence that can be derived

HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. 简单的区间dp,哎,以为很神奇的东西,其实也是dp,只是参数改为区间,没做过此类型的题,想不到用dp,以后就 知道了,若已经知道[0,i],推[0,i+1], 显然还要从i+1 处往回找,dp方程也简单: dp[j][i]=(dp[j+1][i]+dp[j][i-1]+10007-dp[j+1][

HDU 4632 Palindrome subsequence(区间dp)

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others) Total Submission(s): 2595    Accepted Submission(s): 1039 Problem Description In mathematics, a subsequence is a sequence that can be derived

Palindrome subsequence (区间DP)

Palindrome subsequence HDU - 4632 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 subseque

hdu 4632 Palindrome subsequence

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>. (

HDU 4632 Palindrome subsequence(区间DP求回文子序列数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设dp[i][j]为[i,j]的回文子序列数,那么得到状态转移方程: dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+MOD)%MOD if(str[i]==str[j]) dp[i][j]+=dp[i-1][j+1]+1 代码: 1 #include<cst