Palindrome subsequence

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

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
 
有两个点要学习
1. 对于计数类的区间dp 这个状态转移方程比较有意思
dp[i][j] = (dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1] + mod) % mod;
具体理解这个转移方程 自己枚举几个样例就知道了 至于怎么想到这个的
对于s[i]!=s[j]的情况 dp[i][j]所有情况是 dp[i][j-1],dp[i+1][j]这个两个状态的交集 (不得不说一下,由于是枚举所有的情况,这个理解可以从 求i~j这个区间的所有子序列个数归纳出来)
然后当s[i] == s[j]的时候(或者说我们之考虑 dp[i][j-1],dp[i+1][j]两个状态交集的时候),解是不全的 漏下的解是当j 可以和dp[i][j-1]或者 i可以和dp[i+1][j-1]的回文子串构成新的回文字串的情况 这两重情况去重以后 就可以写成
if(a[i] == a[j]) dp[i][j] = (dp[i][j] + dp[i+1][j-1] + 1) % mod;
2.就是。。 取模的时候 如果有减法。 一定要加上模数啊。。 负数wa死你
#include<cstdio>
#include<iostream>
#include<cstring> using namespace std;
const int maxn = 1005;
const int mod = 10007; char a[maxn];
int n,dp[maxn][maxn]; int solve(){
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++) dp[i][i]=1;
for(int i = n-2 ; i >= 0 ;i--)// 由于dp[i][j]依赖 dp[i+1] 所以枚举区间的时候,我们要从大到小枚举i
{
for(int j = i+1 ;j < n ; j++)
{
dp[i][j] = (dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1] + mod) % mod;
if(a[i] == a[j]) dp[i][j] = (dp[i][j] + dp[i+1][j-1] + 1) % mod;
}
}
return dp[0][n-1];
} int main(){
int cas;
scanf("%d",&cas);
for(int T = 1 ; T <= cas; T++){
scanf("%s",a);
n = strlen(a);
printf("Case %d: %d\n",T,solve());
} return 0;
}

  

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. CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)

    Times:5000ms: Memory limit:262144 kB 给定字符串S(|S|<=5000),下标由1开始.然后Q个问题(Q<=1e6),对于每个问题,给定L,R,回答区间 ...

  4. Hdu 3068 最长回文字串Manacher算法

    题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. 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+ ...

  6. LightOJ - 1205:Palindromic Numbers (数位DP&回文串)

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  7. P1435 回文字串

    P1435 回文字串 题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最 ...

  8. 求字符串的最长回文字串 O(n)

    昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...

  9. hihocoder 第一周 最长回文字串

    题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...

随机推荐

  1. svg简单的应用

    1.可以直接在html内写svg (1)width宽度,height高度 (2)xmlns svg的规则 <svg xmlns="http://www.w3.org/2000/svg& ...

  2. <JavaScript>如何阅读《JavaScript高级程序设计》(一)

    题外话 最近在看<JavaScript高级程序设计>这本书,面对着700多页的厚书籍,心里有点压力,所以我决定梳理一下..探究一下到底怎么读这本书.本书的内容好像只有到ES5...所以只能 ...

  3. 前端三大框架(Angular Vue React)

    前端,HTML(超文本标记语言),CSS(层叠样式表)和JavaScript(脚本语言) HTML,通常说的h5,其实按标准来说,HTML4的后续版本不带编号了,并保证向前的兼容性 CSS的版本3,增 ...

  4. gitlib的安装

    下载ruby yum -y install gcc gcc-c++ make wget https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.9.tar. ...

  5. a lot of attention under the hood

    Because one of the original goals of the Node.js project was to allow developers to easily build app ...

  6. wpf相关好资源

    Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in-WPF.aspx Odys ...

  7. Qt编写自定义控件13-多态进度条

    前言 多态进度条,顾名思义,有多重状态,其实本控件主要是用来表示百分比进度的,由于之前已经存在了百分比进度条控件,名字被霸占了,按照先来先得原则,只好另外取个别名叫做多态进度条,应用场景是,某种任务有 ...

  8. HashMap ArrayList 和 List对象的转换

    public static void main(String[] args) { List list = new ArrayList<>(); HashMap map = new Hash ...

  9. [Spark] Scala programming - basic level

    环境配置 IDE: https://www.jetbrains.com/idea/ 子雨大数据之Spark入门教程(Scala版) /* implement */ 语言特性 Online compil ...

  10. 手写web框架之加载Controller,初始化框架

    1,加载Controller     我们需要创建 一个ControllerHelper类,让它来处理下面的逻辑:      通过ClassHelper我们可以获取所有定义了Controller注解的 ...