Palindrome subsequence

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

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
 
 

题意:找出一个串中所有"回文串"的个数。这个回文串的意思是:所有子串(包裹不相邻的)只要满足回文串的性质都属于回文串.

比如 acba 里面总共有 5个回文串 a,c,b,a,aa
 
题解:区间DP,对于 i - j 区间,它满足条件的子串数量为 dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] (子问题之和-重复子问题)
如果str[i] ==str[j] 的话 ,那么在此基础上 满足条件的回文串数量还要加上 dp[i+1][j-1]+1 .
因为 对于 i<t<=k<j  如果 str[t]和str[k] 组成回文串 那么  '字符M'+str[t] 和 str[k]+'字符M'又是一个新的回文串,加上本身自己又是个回文串,所以要
加上 dp[i+1][j-1]+1
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#define N 1005
using namespace std; int dp[][];
char str[];
int main()
{
int tcase;
int k = ;
scanf("%d",&tcase);
while(tcase--){
scanf("%s",str+);
int len = strlen(str+);
for(int i=;i<=len;i++) dp[i][i]=;
for(int l=;l<=len;l++){
for(int i=;i<=len-l+;i++){
int j=i+l-;
dp[i][j] = (dp[i+][j]+dp[i][j-]-dp[i+][j-]+)%;///子问题推出父问题(减掉重复子问题)
///有减号要加mod防止出现负数
if(str[i]==str[j]){
dp[i][j] = (dp[i][j]+dp[i+][j-]+)%;///如果str[i]str[j]相等,那么会多出dp[i+1][j-1]+1个回文串
}
}
}
printf("Case %d: %d\n",k++,dp[][len]);
}
return ;
}

hdu 4632(区间dp)的更多相关文章

  1. HDU 4632 区间DP 取模

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...

  2. hdu 4632区间dp 回文字串计数问题

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  3. hdu 4632区间 dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 用点容斥原理转移状态, dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+ ...

  4. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  6. HDU 4293---Groups(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...

  7. String painter HDU - 2476 -区间DP

    HDU - 2476 思路:分解问题,先考虑从一个空串染色成 B串的最小花费 ,区间DP可以解决这个问题 具体的就是,当 str [ l ] = = str [ r ]时 dp [ L ] [ R ] ...

  8. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. HDU 4570(区间dp)

    E - Multi-bit Trie Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. ACE日志系统

    引用于:http://blog.csdn.net/focusonace/article/details/3108873 http://peirenlei.iteye.com/blog/305036 介 ...

  2. Codeforces Round #331 (Div. 2) A

    A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. [codeforces/gym/101350/L]维护“凸包”

    题目链接:http://codeforces.com/gym/101350/problems 给定n个墙,每个墙有一个高度,要支持动态修改墙的高度和查询这个“容器”能盛多少水. (队友)观察发现,能盛 ...

  4. Sightseeing(dijlstar) 计算最短路和次短路的条数

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10004   Accepted: 3523 Desc ...

  5. [ldap]ldap相关问题

    背景: ldap数据库要同步,按照如下操作步骤: 1.导出: 使用slapcat,slapcat直接对数据库操作, slapcat 2.将所需的条目取出,生成文件in.ldif 3.在目标机器上导入: ...

  6. [解决] User [dr.who] is not authorized to view the logs for application

    在hadoop集群启用权限控制后,发现job运行日志的ui访问不了, User [dr.who] is not authorized to view the logs for application ...

  7. windows版本redis下载安装

    官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 在官方下载网页中有一个window版本的说明,说redis官方没有wi ...

  8. HTML或者JSP页面--执行完某事件后刷新页面,重置表单,清空数据

    在提交表单或者执行某个事件之后,如果需要重置表单(即清空表单里的数据) 可以执行下面代码来完成 方式一: self.location.href="userController.do?goAd ...

  9. 函数式编程--响应式编程 ---android应用例子

    RxJava implements this operator as create. It is good practice to check the observer’s isUnsubscribe ...

  10. 【bzoj1774-过路费】floyd+排序

    题意:n个点,m条双向边,每个点有权值c[i],每条边有权值a[i].d,一条路径的费用=每条边的权值和+各个点的权值的最大值,即sigma(a[i].d)+max(c[i]).q个询问,问x到y的最 ...