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 ...
随机推荐
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- python环境中运行程序
运行Python程序,我们比较常用的是直接在Windows命令提示窗口或者Linux终端或shell窗口中,直接:Python *.py,或者在Linux环境下,在投不中,加入: #!/usr/bin ...
- 【转】drupal7请求异常,执行时间过长的解决方法
drupal7请求错误,执行时间过长的解决办法 根据你的系统或网络设置Drupal不能读取网页,造成功能缺失.可能是web服务器配置或PHP设置引起的,可用更新.获取更新源.使用OpenID登 录或使 ...
- C#接口的使用场合,接口应用
当一个项目不断的扩大的时候,会面临的问题是不断的有以下情况: 1.以前编写程序的人离职了,新来的程序员看不懂以前的程序,或者觉得以前的程序部够好,但又不希望删除: 2.当实现第三方接口时,如:读写IC ...
- ubuntu 下安装 lxml 失败
/tmp/pip-build-7HN4t8/lxml/src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: N ...
- Unity3d 在不同设备中的文件读写 的路径
Application.dataPath : 数据路径 Unity Editor: <path tp project folder>/Assets Unity 编辑器:<工程文件 ...
- 字节对齐导致的iOS EXC_ARM_DA_ALIGN崩溃
本文原链接: http://www.cnblogs.com/zouzf/p/4455167.html 先看一下这个链接:http://www.cnblogs.com/ren54/archive/201 ...
- lxde桌面默认快捷键
ctrl+alt+左右 选择左右桌面shift+alt+左右 当前窗口送至左右桌面房子键+F1~F4 切换桌面1-4房子键+d 显示桌面alt+esc ...
- Android对话框之Context
代码就这么一段: new AlertDialog.Builder(getApplicationContext(),R.style.MyAlertDialogStyle) .setTitle(" ...
- java--遍历自定义数组
比如像下面这样 for (int i : new int[]{1,4,8}){ System.out.println(i); } 或者这样: for (String i : new String[]{ ...