Queries for Number of Palindromes

Problem's Link:   http://codeforces.com/problemset/problem/245/H


Mean:

给你一个字符串,然后q个询问:从i到j这段字符串中存在多少个回文串。

analyse:

dp[i][j]表示i~j这段的回文串数。

首先判断i~j是否为回文,是则dp[i][j]=1,否则dp[i][j]=0;

那么dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i+1[j]-dp[i+1][j-1],从后往前推;

注意判断dp[i][j]是否是回文也需要从后往前推,否则超时。

Time complexity: O(n*n)

Source code: 

  1. // Memory Time
  2. // 1347K 0MS
  3. // by : crazyacking
  4. // 2015-03-31-16.17
  5. #include<map>
  6. #include<queue>
  7. #include<stack>
  8. #include<cmath>
  9. #include<cstdio>
  10. #include<vector>
  11. #include<string>
  12. #include<cstdlib>
  13. #include<cstring>
  14. #include<climits>
  15. #include<iostream>
  16. #include<algorithm>
  17. #define MAXN 5005
  18. #define LL long long
  19. using namespace std;
  20. char str[MAXN];
  21. int dp[MAXN][MAXN];
  22.  
  23. bool judge(int sta,int en)
  24. {
  25. for(int i=sta,j=en;i<j;++i,--j)
  26. {
  27. if(str[i]!=str[j])
  28. return false;
  29. }
  30. return true;
  31. }
  32. int main()
  33. {
  34. gets(str);
  35. int len=strlen(str);
  36. memset(dp,,sizeof dp);
  37. for(int i=;i<len;++i)
  38. {
  39. if(str[i]==str[i+])
  40. dp[i][i+]=;
  41. dp[i][i]=;
  42. }
  43. for(int i=len-;i>=;--i)
  44. {
  45. for(int j=i;j<len;++j)
  46. {
  47. if(dp[i+][j-]==&&str[i]==str[j])
  48. dp[i][j]=;
  49. }
  50. }
  51. for(int i=len-;i>=;--i)
  52. {
  53. for(int j=i;j<len;++j)
  54. {
  55. dp[i][j]=dp[i][j]+dp[i][j-]+dp[i+][j]-dp[i+][j-];
  56. }
  57. }
  58. int q;
  59. scanf("%d",&q);
  60. while(q--)
  61. {
  62. int x,y;
  63. scanf("%d %d",&x,&y);
  64. printf("%d\n",dp[x-][y-]);
  65. }
  66. return ;
  67. }

dp --- Codeforces 245H :Queries for Number of Palindromes的更多相关文章

  1. codeforces 245H Queries for Number of Palindromes RK Hash + dp

    H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...

  2. Codeforces 245H Queries for Number of Palindromes:区间dp

    题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的 ...

  3. codeforces 245H . Queries for Number of Palindromes 区间dp

    题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...

  4. Codeforces 245H Queries for Number of Palindromes

    http://codeforces.com/contest/245/problem/H 题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000) 思路:设定pd[i][j]代表 ...

  5. codeforces H. Queries for Number of Palindromes(区间dp)

    题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b ...

  6. Queries for Number of Palindromes (区间DP)

    Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...

  7. Queries for Number of Palindromes(求任意子列的回文数)

    H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...

  8. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  9. K - Queries for Number of Palindromes(区间dp+容斥)

    You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There a ...

随机推荐

  1. [Aaronyang] 写给自己的WPF4.5 笔记19[Visual类图文并茂讲解]

    文章虽小,内容还好,且看且珍惜. aaronyang版权所有,不许转载,违者必究 当界面上使用数千个矢量图形,例如实时统计图,粒子碰撞,比如超级玛丽游戏,图像一直在绘,过量的使用WPF的元素系统和Sh ...

  2. Android Studio 快捷键 for mac

    Action Mac OS Win/Linux 打开文件 Cmd + shift + O   打开Class文件 Cmd + O   覆写方法 Ctrl + O   生成方法(重写构造.setter ...

  3. 如何使Session永不过期

    转载:http://blog.csdn.net/wygyhm/article/details/2819128 先说明情况:公司做监控系统,B/S结构,主要用在局域网内部!监控系统开机可能要开好长时间, ...

  4. 解决 The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path

    到 http://tomcat.heanet.ie/native/ 下载最新的tcnative-1.dll放到相应目录即可,我目前下载的是 http://tomcat.heanet.ie/native ...

  5. 用户管理 之 Linux 系统中的超级权限的控制

    在Linux操作系统中,root的权限是最高的,也被称为超级权限的拥有者.普通用户无法执行的操作,root用户都能完成,所以也被称之为超级管理用户. 在系统中,每个文件.目录和进程,都归属于某一个用户 ...

  6. Intellij idea 乱码问题(英文操作系统)

    英文操作系统导致 Debug 下的变量查看时显示乱码,可通过改变字体解决此问题.

  7. 出现Assertion failure in -[***** layoutSublayersOfLayer:]

    在自定义的view中使用了[self layoutIfNeeded]方法,在iOS8\9上都没有错误,但是在iOS7上出现了页面错乱,解决方案就是在自定义的view里面添加如下代码: + (void) ...

  8. 在linux下安装Xwindows

    检查Linux系统是否能够联网. 执行命令 yum -y groupinstall Desktop 等上面的命令执行完后,再执行这条命令 yum -y groupinstall "X Win ...

  9. 实战:ajax带参数请求slim API

    restful api 支持get,post,put,delete等方法,那么jquery客户端怎么去实现呢?涉及到跨域又怎么办? 很多时候需要传递一个token(api_key) 去识别用户身份,获 ...

  10. jsp页面间传递参数 中文乱码问题(zz)

      jsp页面间传递参数 中文乱码问题 1.传递参数 var url = "*****Test.jsp?param1="+encodeURI(encodeURI(str));//对 ...