codeforces 245H Queries for Number of Palindromes RK Hash + dp
5 seconds
256 megabytes
standard input
standard output
You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are qqueries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes.
String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|.
String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1.
The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106)— the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query.
It is guaranteed that the given string consists only of lowercase English letters.
Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.
caaaba
5
1 1
1 4
2 3
4 6
4 5
1
7
3
4
2
Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba».
大意:长度为N的字符串,Q个询问,询问某个区间中回文子串(连续)的数量
题解:
将字符串和反转串的RK hash值求出,这样可以O(1)判断两个子串是否相等。
f[i][j]表示[i,j]区间中回文子串的数量,可以以区间大小为阶段利用简单容斥来递推。
f[i][j]=f[i+1][j]+f[i][j-1]-f[i+1][j-1]+([i,j]是回文串)
要[i,j]是回文串,只需在原串中求出前半部分的 hash 值,在反转串中求出后半串的 hash 值,判断是否相等即可。
tips:亲测O(N^2 logN)过不了,要预处理seed的幂次方
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=xx*+ch-'';ch=getchar();}
return xx*ff;
}
const int seed[]={,},MOD=;
char s1[],s2[];
int H1[][],H2[][];
int N;
int pow_seed[][];
void Hashing(int x){
for(int i=;i<=N;i++){
H1[x][i]=(1LL*H1[x][i-]*seed[x]+s1[i])%MOD;
H2[x][i]=(1LL*H2[x][i-]*seed[x]+s2[i])%MOD;
}
}
int get_hash1(int x,int L,int R){
return ((H1[x][R]-1LL*H1[x][L-]*pow_seed[x][R-L+])%MOD+MOD)%MOD;
}
int get_hash2(int x,int L,int R){
return ((H2[x][R]-1LL*H2[x][L-]*pow_seed[x][R-L+])%MOD+MOD)%MOD;
}
inline int ref(int x)
{return N-x+;}
inline bool judge(int L,int R){
if(L==R)
return ;
int mid=(L+R)/;
if((R-L+)%==){
if(get_hash1(,L,mid)==get_hash2(,ref(R),ref(mid+)))
if(get_hash1(,L,mid)==get_hash2(,ref(R),ref(mid+)))
return ;
}
else{
if(get_hash1(,L,mid)==get_hash2(,ref(R),ref(mid)))
if(get_hash1(,L,mid)==get_hash2(,ref(R),ref(mid)))
return ;
}
return ;
}
long long f[][];
int main(){
//freopen("in","r",stdin);
gets(s1+);N=strlen(s1+);
for(int i=;i<=N;i++)
s2[i]=s1[ref(i)];
pow_seed[][]=pow_seed[][]=;
for(int i=;i<=N;i++){
pow_seed[][i]=1LL*pow_seed[][i-]*seed[]%MOD;
pow_seed[][i]=1LL*pow_seed[][i-]*seed[]%MOD;
}
Hashing();
Hashing();
for(int i=;i<=N;i++)
f[i][i]=;
for(int p=;p<=N;p++)
for(int i=;i<=N;i++){
int j=i+p-;
if(j>N)
break;
f[i][j]=f[i+][j]+f[i][j-]-f[i+][j-]+judge(i,j);
}
/*for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++)
printf("%I64d ",f[i][j]);
puts("");
}*/
for(int Q=read();Q;Q--){
int t1=read(),t2=read();
printf("%I64d\n",f[t1][t2]);
}
return ;
}
codeforces 245H Queries for Number of Palindromes RK Hash + dp的更多相关文章
- Codeforces 245H Queries for Number of Palindromes:区间dp
题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的 ...
- 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
http://codeforces.com/contest/245/problem/H 题意:给定一个字符串,每次给个区间,求区间内有几个回文串(n<=5000) 思路:设定pd[i][j]代表 ...
- 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 ...
- 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(区间dp)
You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There a ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- Queries for Number of Palindromes(求任意子列的回文数)
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
随机推荐
- [Python3网络爬虫开发实战] 1.7.2-mitmproxy的安装
mitmproxy是一个支持HTTP和HTTPS的抓包程序,类似Fiddler.Charles的功能,只不过它通过控制台的形式操作. 此外,mitmproxy还有两个关联组件,一个是mitmdump, ...
- SQL中带有NOT IN 子查询改写
报表程序中的一段SQL运行很慢,代码如下: 优化前: 耗时:1337s INSERT INTO PER_LTE_ZIB_PB_COMMISSION_07 SELECT P.TOPACTUALID, Q ...
- xphrof性能分析线上部署实践
说明 将xhprof部署在线上环境,在特定情况下进行性能分析,方便快捷的排查线上性能问题. 通过参数指定及添加代码行触发进入性能分析,并将结果保存入MongoDB. 因为xhprof对性能的影响,只部 ...
- PAT顶级 1002. Business (35)
PAT顶级 1002. Business (35) As the manager of your company, you have to carefully consider, for each p ...
- Linux基础命令回顾
前言 说到linux基础命令,网上一搜一箩筐,想学也有很多教程,如果你不幸看到此篇文章,想看就认真看完,毕竟你点进来了不是嘛? 我每次写的文章都是为了分享自己的学习成果或重要知识点,希望能帮助更多的人 ...
- 又一个ajax实例,结合jQuery
又一个ajax实例,配合jQuery html <!DOCTYPE html> <html lang="zh-cn"> <head> < ...
- HTML5 & CSS3 & font-family
HTML5 & CSS3 & font-family 中文字体的英文名称 宋体* SimSun 黑体* SimHei 微软雅黑* Microsoft YaHei 微软正黑体 http: ...
- Maven 引入外部包
当需要从外部引入一个包(譬如说读写Excel 的POI jar 包 ), 不需要手动地去官网下载一个包然后粘贴到相应的地方. 只需要把Java 工程 转为 Maven 工程 ( 在工程上右键 > ...
- [bzoj3160]万径人踪灭_FFT_Manacher
万径人踪灭 bzoj-3160 题目大意:给定一个ab串.求所有的子序列满足:位置和字符都关于某条对称轴对称而且不连续. 注释:$1\le n\le 10^5$. 想法: 看了大爷的题解,OrzOrz ...
- cogs——21. [HAOI2005] 希望小学
21. [HAOI2005] 希望小学 ★★ 输入文件:hopeschool.in 输出文件:hopeschool.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述 ...