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 ...
随机推荐
- Navi.Soft30.开放平台.腾讯.开发手册
1系统简介 1.1功能简述 现在是一个信息时代,并且正在高速发展.以前获取信息的途径非常少,可能只有电视台,收音机等有限的来源,而现在的途径数不胜数,如:QQ,微信,官方网站,个人网站等等 本开发手册 ...
- [原创]android自定义控件的最大高度MaxHeightView
代码地址:https://github.com/Carbs0126/MaxHeightView android中部分控件具有maxHeight功能,如button等,但是对于ViewGroup类的控件 ...
- Unity中Mesh分解与边缘高亮加上深度检测
一个比较简单的需求,不过遇到些坑,记录下. 房间有多个模型,每个模型可能多个SubMesh,点击后,需要能具体到是那个SubMesh,并且在这个SubMesh上显示边缘高光,以及能个性这单个SubMe ...
- 【Cocos2d-Js基础教学(5)资源打包工具的使用及资源的异步加载处理】
TexturePacker是纹理资源打包工具,支持Cocos2dx的游戏资源打包. 如果用过的同学可以直接看下面的资源的异步加载处理 首先为什么用TexturePacker? 1,节省图片资源实际大小 ...
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- SQL2008数据库优化常用脚本
--查询某个数据库的连接数 select count(*) from Master.dbo.SysProcesses where dbid=db_id() --前10名其他等待类型 SELECT TO ...
- English Metric Units and Open XML
English Metric Units and Open XML 在Open XML里使用了English Metric Units(EMUs)来作为度量单位.比如 public class Ext ...
- OpenGL cubeMap
glsl 的reflect(I,N)其中I是 眼睛(camera)位置到顶点位置的方向向量,N为顶点法线,必须要归一化 橙宝书里给出的计算过程是这样的:reflect(I,N) = I - 2 *do ...
- Android ListView OnItemLongClick和OnItemClick事件内部细节分享以及几个比较特别的属性
本文转自 http://blog.sina.com.cn/s/blog_783ede030101bnm4.html 作者kiven 辞职3,4个月在家休息,本以为楼主要程序员逆袭,结果失败告终继续码农 ...
- AjaxUploader使用
一.简单上传(Simple Upload with Progress) <CuteWebUI:Uploader ID="ajaxUploader" runat="s ...