dp --- Codeforces 245H :Queries for Number of Palindromes
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:
- // Memory Time
- // 1347K 0MS
- // by : crazyacking
- // 2015-03-31-16.17
- #include<map>
- #include<queue>
- #include<stack>
- #include<cmath>
- #include<cstdio>
- #include<vector>
- #include<string>
- #include<cstdlib>
- #include<cstring>
- #include<climits>
- #include<iostream>
- #include<algorithm>
- #define MAXN 5005
- #define LL long long
- using namespace std;
- char str[MAXN];
- int dp[MAXN][MAXN];
- bool judge(int sta,int en)
- {
- for(int i=sta,j=en;i<j;++i,--j)
- {
- if(str[i]!=str[j])
- return false;
- }
- return true;
- }
- int main()
- {
- gets(str);
- int len=strlen(str);
- memset(dp,,sizeof dp);
- for(int i=;i<len;++i)
- {
- if(str[i]==str[i+])
- dp[i][i+]=;
- dp[i][i]=;
- }
- for(int i=len-;i>=;--i)
- {
- for(int j=i;j<len;++j)
- {
- if(dp[i+][j-]==&&str[i]==str[j])
- dp[i][j]=;
- }
- }
- for(int i=len-;i>=;--i)
- {
- for(int j=i;j<len;++j)
- {
- dp[i][j]=dp[i][j]+dp[i][j-]+dp[i+][j]-dp[i+][j-];
- }
- }
- int q;
- scanf("%d",&q);
- while(q--)
- {
- int x,y;
- scanf("%d %d",&x,&y);
- printf("%d\n",dp[x-][y-]);
- }
- return ;
- }
dp --- Codeforces 245H :Queries for Number of Palindromes的更多相关文章
- 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 ...
- Codeforces 245H Queries for Number of Palindromes:区间dp
题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的 ...
- 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] ...
- Codeforces 245H Queries for Number of Palindromes
http://codeforces.com/contest/245/problem/H 题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000) 思路:设定pd[i][j]代表 ...
- codeforces H. Queries for Number of Palindromes(区间dp)
题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b ...
- Queries for Number of Palindromes (区间DP)
Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...
- Queries for Number of Palindromes(求任意子列的回文数)
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 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 ...
随机推荐
- [Aaronyang] 写给自己的WPF4.5 笔记19[Visual类图文并茂讲解]
文章虽小,内容还好,且看且珍惜. aaronyang版权所有,不许转载,违者必究 当界面上使用数千个矢量图形,例如实时统计图,粒子碰撞,比如超级玛丽游戏,图像一直在绘,过量的使用WPF的元素系统和Sh ...
- Android Studio 快捷键 for mac
Action Mac OS Win/Linux 打开文件 Cmd + shift + O 打开Class文件 Cmd + O 覆写方法 Ctrl + O 生成方法(重写构造.setter ...
- 如何使Session永不过期
转载:http://blog.csdn.net/wygyhm/article/details/2819128 先说明情况:公司做监控系统,B/S结构,主要用在局域网内部!监控系统开机可能要开好长时间, ...
- 解决 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 ...
- 用户管理 之 Linux 系统中的超级权限的控制
在Linux操作系统中,root的权限是最高的,也被称为超级权限的拥有者.普通用户无法执行的操作,root用户都能完成,所以也被称之为超级管理用户. 在系统中,每个文件.目录和进程,都归属于某一个用户 ...
- Intellij idea 乱码问题(英文操作系统)
英文操作系统导致 Debug 下的变量查看时显示乱码,可通过改变字体解决此问题.
- 出现Assertion failure in -[***** layoutSublayersOfLayer:]
在自定义的view中使用了[self layoutIfNeeded]方法,在iOS8\9上都没有错误,但是在iOS7上出现了页面错乱,解决方案就是在自定义的view里面添加如下代码: + (void) ...
- 在linux下安装Xwindows
检查Linux系统是否能够联网. 执行命令 yum -y groupinstall Desktop 等上面的命令执行完后,再执行这条命令 yum -y groupinstall "X Win ...
- 实战:ajax带参数请求slim API
restful api 支持get,post,put,delete等方法,那么jquery客户端怎么去实现呢?涉及到跨域又怎么办? 很多时候需要传递一个token(api_key) 去识别用户身份,获 ...
- jsp页面间传递参数 中文乱码问题(zz)
jsp页面间传递参数 中文乱码问题 1.传递参数 var url = "*****Test.jsp?param1="+encodeURI(encodeURI(str));//对 ...