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死你
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6. const int maxn = 1005;
  7. const int mod = 10007;
  8.  
  9. char a[maxn];
  10. int n,dp[maxn][maxn];
  11.  
  12. int solve(){
  13. memset(dp,0,sizeof(dp));
  14. for(int i=0;i<n;i++) dp[i][i]=1;
  15. for(int i = n-2 ; i >= 0 ;i--)// 由于dp[i][j]依赖 dp[i+1] 所以枚举区间的时候,我们要从大到小枚举i
  16. {
  17. for(int j = i+1 ;j < n ; j++)
  18. {
  19. dp[i][j] = (dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1] + mod) % mod;
  20. if(a[i] == a[j]) dp[i][j] = (dp[i][j] + dp[i+1][j-1] + 1) % mod;
  21. }
  22. }
  23. return dp[0][n-1];
  24. }
  25.  
  26. int main(){
  27. int cas;
  28. scanf("%d",&cas);
  29. for(int T = 1 ; T <= cas; T++){
  30. scanf("%s",a);
  31. n = strlen(a);
  32. printf("Case %d: %d\n",T,solve());
  33. }
  34.  
  35. return 0;
  36. }

  

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. moveLeft()

    这里大致都和上面一样,就是在记录左边坐标时,应该应该是lx = x - 1. void moveLeft(){ //定义变量存放人物左边的坐标 int lx, ly; //当左边没有元素时,直接ret ...

  2. Leetcode题目104.二叉树的最大深度(DFS+BFS简单)

    题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...

  3. Alpha冲刺(6/6)

    队名:new game 组长博客:戳 作业博客:戳 组员情况 鲍子涵(队长) 燃尽图 过去两天完成了哪些任务 协调了一下组内的工作 复习了一下SuffixAutomata 接下来的计划 实现更多的功能 ...

  4. python 文件压缩及解压

    文件压缩 import os import zipfile def zip_dir(dirname,zipfilename): """ | ##@函数目的: 压缩指定目录 ...

  5. ios 报错记录

    1. 运行xcode 报错:unterminated conditional directive #ifdef 缺少对应的#endif 在结尾加上就好了 2.iOS添加非(c,c++)文件引发的&qu ...

  6. backbone之module

    上一篇列出了collection的代码,下面要把代码进行分离 //先是app.js var ContactManager = new Marionette.Application(); Contact ...

  7. iOS开发嵌套ReactNative页面

    最近使用ReactNative做项目,有信心今天目标把ReactNative框架掌握,所以自己从每个知识点学习提高自己吧...... 步骤如下: 一.创建依赖包文件(package.json): Re ...

  8. Kubernetes架构

    一.Kubernetes 架构: Kubernetes Cluster 由 Master 和 Node 组成,节点上运行着若干 Kubernetes 服务. 1. Master 节点 Master 是 ...

  9. SQL引擎及事务支持

    查看当前表引擎 SHOW CREATE TABLE table_name MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持.MyISAM类型的表强调的是性能,其执行数度比InnoDB类 ...

  10. 逆序对 -- cogs1438 火柴排队

    题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=vimiQkqjU [题目描述] 样例一输入: 4 2 3 1 4 3 2 1 4 样例二 ...