Codeforces 245H Queries for Number of Palindromes
http://codeforces.com/contest/245/problem/H
题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000)
思路:设定pd[i][j]代表i~j这部分是不是一个回文串,这个可以n^2预处理
然后设定f[i][j]代表i~j区间有多少个回文串,由于满足区间加减,因此有
f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1]+pd[i][j]
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int pd[][],f[][],n;
char s[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void init(){
for (int i=;i<=n;i++)
pd[i][i]=;
for (int len=;len<=n;len++)
for (int i=;i+len-<=n;i++){
int j=i+len-;
if (i==j-&&s[i]==s[j]) pd[i][j]=;
else
if (s[i]==s[j]) pd[i][j]|=pd[i+][j-];
}
for (int i=;i<=n;i++)
f[i][i]=;
for (int i=;i<n;i++)
if (s[i]==s[i+])
f[i][i+]=;
else
f[i][i+]=;
for (int len=;len<=n;len++)
for (int i=;i+len-<=n;i++){
int j=i+len-;
f[i][j]=f[i+][j]+f[i][j-]-f[i+][j-]+pd[i][j];
}
}
int main(){
scanf("%s",s+);
n=strlen(s+);
init();
int T=read();
while (T--){
int l=read(),r=read();
printf("%d\n",f[l][r]);
}
return ;
}
Codeforces 245H Queries for Number of Palindromes的更多相关文章
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- 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 H. Queries for Number of Palindromes(区间dp)
题目链接:http://codeforces.com/contest/245/problem/H 题意:给出一个字符串还有q个查询,输出每次查询区间内回文串的个数.例如aba->(aba,a,b ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 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(回文树)
题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...
随机推荐
- Codeforces 219D Choosing Capital for Treeland
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
- CCI_chapter 16 Low level
16.5 Write a program to find whether a machine is big endian or little endian Big-Endian和Little-Endi ...
- 复制文件时,如何显示进度条(使用TFileStream一点一点读,或者使用BlockRead)
procedure mycopyfile(sourcef,targetf:string;i:integer); var FromF,ToF:file; NumRead,NumWritten:Integ ...
- C# 二进制,十进制,十六进制
//十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...
- bzoj1149
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1149 水题..... 直接BFS. #include<cstdio> #inclu ...
- poj:2992 因子数量
题意: 求 组合数c(n,k)的因子数量 由算术基本定理很容易求得,不过第一次却T了,加了好多预处理,o1查询,才过 #include <iostream> #include <st ...
- SVN 一次性提交多个目录中文件
情况一:将项目中未加入版本控制的文件提交到版本库. 在使用WINDOW下的SVN客户端工具时,在提交一个项目的文件时,如果有未加入版本库的文件,这时可以先将未加入的文件选中,然后一起提交. 但在LIN ...
- C#生成带项目编号的Word段落
using System; using Microsoft.Office.Interop.Word; using Word = Microsoft.Office.Interop.Word; names ...
- [置顶] Android安全机制分析
Android系统是基于Linux内核开发的,因此,Android系统不仅保留和继承了Linux操作系统的安全机制,而且其系统架构的各个层次都有独特的安全特性[2] . 1. Linux内核层安全机制 ...
- Winform改变Textbox边框颜色(转)
namespace MyTextBoxOne { //使用时必须把文本框的BorderStyle为FixedSingle才能使用 //一些控件(如TextBox.Button等)是由系统进程绘制,重载 ...