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. Python爬虫学习笔记之微信宫格验证码的识别(存在问题)

    本节我们将介绍新浪微博宫格验证码的识别.微博宫格验证码是一种新型交互式验证码,每个宫格之间会有一条 指示连线,指示了应该的滑动轨迹.我们要按照滑动轨迹依次从起始宫格滑动到终止宫格,才可以完成验证,如 ...

  2. CSS实现三列布局方法总结

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABYwAAAI7CAYAAABPx9+YAAARJElEQVR4nO3cwWnDQBBA0TioJrXhTl

  3. ZooKeeper文档(二)

    ZooKeeper:因为协调的分布式系统是一个动物园 ZooKeeper对分布式应用来说是一个高性能的协调服务.它暴露通常的服务-比如命名,配置管理,同步,和组服务-用一种简单的接口,所以你不用从头开 ...

  4. Spring 学习笔记 整合 Struts2

           Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spri ...

  5. DataGridView导出到Word

    #region 使用Interop.Word.dll将DataGridView导出到Word /// <summary> /// 使用Interop.Word.dll将DataGridVi ...

  6. Installing JDK7 on Ubuntu

    1. # sudo add-apt-repository ppa:webupd8team/java 2.  # sudo apt-get update 3.  # sudo apt-get insta ...

  7. 归并排序Merge sort2

    原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...

  8. 【Codeforces370E】Summer Reading [构造]

    Summer Reading Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 7 0 1 ...

  9. 【BZOJ】1588: [HNOI2002]营业额统计

    [算法]平衡树(treap)||双向链表 [题解]treap知识见数据结构. #include<cstdio> #include<algorithm> #include< ...

  10. 大聊Python----进程和线程

    什么是线程? 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务. ...