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. 【状压DP】【UVA11825】 Hackers' Crackdown

    传送门 Description 你是一个hacker,侵入了一个有着n台计算机(编号为1.2.3....n)的网络.一共有n种服务,每台计算机都运行着所有服务.对于每台计算机,你都可以选择一项服务,终 ...

  2. [codeforces/edu4]总结(F)

    链接:http://codeforces.com/contest/612/ A题: 枚举切多少个p,看剩下的能否整除q. B题: 从1到n模拟一下,累加移动的距离. C题: 先用括号匹配的思路看是否有 ...

  3. BZOJ1105 [POI2007]石头花园SKA 贪心

    [POI2007]石头花园SKA Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 776  Solved: 237[Submit][Status][Di ...

  4. js 获取当前链接和获取域名

    <script language="javascript"> //获取域名 host = window.location.host; host2=document.do ...

  5. iframe的使用及操作

    一.iframe的使用方法: 在一个页面中加入iframe代码,例如: <div class="myiframe"> <iframe src="test ...

  6. ZooKeeper概述(三)

    ZooKeeper:分布式应用的分布协调服务 ZooKeeper是一个为分布式应用提供的分布的开源的协调服务.它暴露一组简单的原子操作,分布式系统可以在这之上为同步,配置管理,和组和命名实现更高级的服 ...

  7. 数据结构:Bitset

    这个东西看起来很棒棒的样子呀 bitset存储二进制数位 bitset就像一个bool类型的数组一样 bitset中的每个元素都能单独被访问 整数类型和布尔数组都能转化成bitset 有关Bitset ...

  8. mvc BundleConfig实现对Css、Js压缩打包加载

    Bundle不是.net Framework框架中的一员,使用Bundle首先要先添加引用,如下: nuget包管理--程序包管理控制台--Install-Package Microsoft.AspN ...

  9. 【poj1901-求区间第k大值(带修改)】树状数组套主席树

    901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 7025  Solved: 2925[Sub ...

  10. 【lydsy1407】拓展欧几里得求解不定方程+同余方程

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...