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 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]=(dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]);
    if(c[i]==c[j]) 那么加上中间的dp[i+1][j-1],因为可以和i,j形成新的
    还要加上  1  (i 和  j 形成字符串)

*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8
typedef __int64 ll;

using namespace std;

#define mod 10007
#define INF 0x3f3f3f3f
#define N 1005

int dp[N][N];
int len;
char c[N];

int main()
{
   int i,j,t,ca=0;
   scanf("%d",&t);
   while(t--)
   {
       scanf("%s",c);
       len=strlen(c);
       memset(dp,0,sizeof(dp));
       for(i=0;i<len;i++)
          dp[i][i]=1;

       for(i=len-1;i>=0;i--)
         for(j=i+1;j<len;j++)
         {
            dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+mod)%mod;
            if(c[i]==c[j])
                dp[i][j]=(dp[i][j]+dp[i+1][j-1]+1+mod)%mod;
         }
         printf("Case %d: %d\n",++ca,(dp[0][len-1]+mod)%mod);
   }
   return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-18 22:25:08

HDU 4632 Palindrome subsequence(区间dp)的相关文章

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): 2610    Accepted Submission(s): 1050 Problem Description In mathematics, a subsequence is a sequence that can be derived

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

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 1159——Common Subsequence(DP)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23279    Accepted Submission(s): 10242 Problem Description A subsequence of a given sequence is the given sequence with some e

HDU 5396 Expression (区间DP)

链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5396 设d[i][j] 代表i~j的答案.区间DP枚举(i, j)区间的断点,如果断点处的操作符是'*',那么该区间的答案可以直接加上d[i][k] *  d[k+1][j],因为乘法分配律可以保证所有的答案都会乘起来.如果是加法,需要加的 就是 左边的答案 乘 右边操作数的阶乘 加上 右边的答案乘左边操作数的阶乘,最后要确定左边操作和右边操作的顺序 因为每个答案里是统计了该区间所有的阶乘情况,因此

hdu 5693 &amp;&amp; LightOj 1422 区间DP

hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3组合成. 区间DP,用dp[i][j]表示在i到j之间可以删除的最大数,枚举区间长度,再考虑区间两端是否满足等差数列(这是考虑两个数的),再i到j之间枚举k,分别判断左端点右端点和k是否构成等差数列(还是考虑两个数的),判断左端点,k,右端点是否构成等差数列(这是老驴三个数的) 1 #include

hdu 4597 Play Game 区间dp

Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4597 Description Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take tur