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>.
(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 =
<S
x1, S
x2, ..., S
xk> and Y = <S
y1, S
y2, ..., S
yk> , if there exist an integer i (1<=i<=k) such
that xi != yi, the subsequence X and Y should be consider different even
if S
xi = S
yi. Also two subsequences with different length should be considered different.

InputThe 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.OutputFor 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这里的子串不需要连续,区间dp,首先每个字符都是长度为1的回文串,然后确定长度为i的连续子串里有多少回文串(i>=2),这样长度为i的子串可以由它的长度为i-1的子串来更新。代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define Max 1002
using namespace std;
int t,dp[Max][Max];
char s[Max];

int main()
{
    scanf("%d",&t);
    int k = 1;
    for(int i = 1;i <= 1001;i ++)
        dp[i][i] = 1;///从i到i长度为1的一个字符是长度为1的
    while(t --)
    {
        scanf("%s",s);
        int len = strlen(s);
        for(int i = 1;i <= len - 1;i ++)
        {
            for(int j = 1;j + i <= len;j ++)///j表示从i开始的子串长度
            {
                dp[j][i + j] = dp[j + 1][i + j] + dp[j][i + j - 1] - dp[j + 1][i + j - 1];///[a,b]区间的用[a+1,b] 和 [a,b-1]区间的更新,这两个区间肯定会较早就确定 但是他们都包含了[a+1,b-1]
                if(s[j - 1] == s[i + j - 1])dp[j][i + j] += dp[j + 1][i + j - 1] + 1;///如果边界两个字符相同那么这两个字符可以组成一个回文串 同时 区间内的回文串加上这两个字符又形成新的回文串
                dp[j][i + j] = (dp[j][i + j] + 10007) % 10007;
            }
        }
        printf("Case %d: %d\n",k ++,dp[1][len]);
    }
}

原文地址:https://www.cnblogs.com/8023spz/p/9061434.html

时间: 2024-10-14 02:05:23

hdu 4632 Palindrome subsequence的相关文章

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)

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

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

POJ 1159 Palindrome &amp;&amp; HDU 1159 Common Subsequence

1.先说说杭电的1159吧! 这道题是基础动规,比较简单! 就是要你求最长的公共子序列(不要优化) 动态转移方程: dp[i+1][j+1]=(a[i]=b[i])?dp[i][j]+1:max(dp[i+1][j],dp[i][j+1]) AC代码: #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 520 char a[N],b[N]; in

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 6156 Palindrome Function(回文数位dp)

题目链接:hdu 6156 Palindrome Function 题意: 给你一个L,R,l,r,问你在[L,R]内在[l,r]进制下有多少数是回文数,然后算一算贡献. 题解: 由于答案和该回文数的最高位有关(因为前导0不算). 考虑dp[i][j][k],表示在i进制下,当前考虑到第j位,该数字的起始点在第k位. 然后开一个数组记录一下前面的数字,做一下记忆化搜索就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) memset(a,b,siz

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 fr

hdu 4632 子字符串统计的区间dp

题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. 简单的区间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][i-1])%10007; 减去中间一段重复的 if(s[i]==s[j])dp[j][i]=(dp[j][i]+dp[j+1][i-