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 ...
随机推荐
- AutoEncoders变种
目录 PCA V.S. Auto-Encoders Denoising AutoEncoders Dropout AutoEncoders PCA V.S. Auto-Encoders deep au ...
- 集训第四周(高效算法设计)G题 (贪心)
G - 贪心 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Desc ...
- safepoint与UseCountedLoopSafepoints
safepoint: JIT编码时,会在代码中所有方法的返回之前,以及所有非counted loop的循环(无界循环)回跳之前放置一个safepoint(counted loop则没有放置safepo ...
- Python接口测试之报告(十五)
在本文章中,主要使用jenkins和编写的自动化测试代码,来生成漂亮的测试报告,关于什么是CI这些 我就不详细的介绍了,这里我们主要是实战为主. 首先搭建java的环境,这个这里不做介绍.搭建好jav ...
- 确定协议-通过分析系统阶段需要知道该系统能不能进行性能测试-Omnipeek
- 南邮CTF密码学,mixed_base64
# -*- coding:utf-8 -*- from base64 import * flag = open("code.txt").readline() # 读取密文 for ...
- JVM 总结
面试 java 虚拟机 jvm 基础 jvm Write Once Run EveryWhere >jar 包可以在任何兼容jvm上运行 >jvm 适配器 屏蔽掉底层差异 >内存管理 ...
- [bzoj3436]小K的农场_差分约束
小K的农场 bzoj-3436 题目大意:给定n个点,每个节点有一个未知权值.现在有m个限制条件,形如:点i比点j至少大c,点i比点j至多大c或点i和点j相等.问是否可以通过给所有点赋值满足所有限制条 ...
- 何为幻读?MySQL又是如何解决幻读的?
一.什么是幻读 在一次事务里面,多次查询之后,查询的结果集的个数不一致的情况叫做幻读.而多出来或者少的哪一行被叫做 幻行 二.为什么要解决幻读 在高并发数据库系统中,需要保证事务与事务之间的隔离性,还 ...
- Ubuntu 16.04安装SoapUI工具进行接口测试(Web Service/WSDL/RESTfull)
SoapUI是一个跨平台接口测试工具,官方提供开源版本和商业版本.可以用来测试WSDL/RESTfull等接口. 替代的工具有JMeter. 一般用于WSDL的接口测试比较多,基于XML的形式,且这类 ...