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. React受控组件和非受控组件

    受控组件和非受控组件主要是用来解决表单组件状态谁来控制的问题.因为用户的输入会反应在界面上,相当于视图的状态发生了变化,而react是通过虚拟DOM比对修改视图的,这里就要决定谁来控制表单组件的状态. ...

  2. SDWebImage的使用说明

    1. 在需要的地方导入头文件 #import "UIImageView+WebCache.h" webCache:网络缓存,几乎目前所有的浏览器都有一个内置的缓存,它们通常利用客户 ...

  3. 2115: [Wc2011] Xor (线性基+dfs)

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 5714  Solved: 2420 题目链接:https://w ...

  4. bzoj 3289 Mato的文件管理 树状数组+莫队

    Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 4325  Solved: 1757[Submit][Status][Discuss ...

  5. jq 数组定义,拼接~~~push应用

    var radio_checked_array = []; $("input[type='radio']").each(function(){ if($(this).attr('c ...

  6. JSTL 入门: 表达式语言

    转载自:http://www.ibm.com/developerworks/cn/java/j-jstl0211/ JavaServer Pages(JSP)是用于 J2EE 平台的标准表示层技术.J ...

  7. HashMap详谈以及实现原理

    (一).HashMap 基于哈希表的 Map 接口的实现 允许使用 null 值和 null 键 HashMap不是线程安全,想要线程安全,Collections类的静态方法synchronizedM ...

  8. windows下用时间戳创建文件名

    英文环境下: echo Archive_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.zip 中文: ...

  9. Item 1----------考虑用静态工厂方法代替构造器

    读书,有时候,我感觉总是有点绕和不具体.我阅读了代码,理解代码后,才有一种理解和把握的感觉. 优点三.   把某个对象的构建放给客户端来实现. 比如下面的实现,客户端Test,获取Service的实例 ...

  10. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...