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 ...
随机推荐
- 14Spring通过注解配置Bean(2)
下面将对13Spring通过注解配置Bean(1)的中Repository.Service.Controller通过注解方式来建立关联. <context:component-scan>元 ...
- c++中的三角函数
c++中想求cos或sin: 1.首先得包含头文件,include<math.h> 2.sin(),cos(),中是弧度数,即若是角度a,则应写成cou<<sin(a*pi/1 ...
- 对Kaldi nnet3进行奇异值分解(SVD)以减小模型大小
用处 基于SVD实现模型压缩以适配低功耗平台 根据nnet3bin/nnet3-copy,nnet3-copy或nnet3-am-copy的"--edits-config" ...
- prometheus监控linux系统
安装node exporter 创建Systemd服务 #vim /etc/systemd/system/node_exporter.service[Unit]Description=mysql_ex ...
- Webdriver测试脚本1(打开网页并打印标题)
案例: 启动火狐浏览器 首页打开博客园页面,打印网页标题,等待3秒 打开百度首页,打印网页标题,再等待2秒 关闭浏览器 from selenium import webdriver from time ...
- NYOJ-851寻找最大数(二),栈贪心!
寻找最大数(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给你一个数字n(可能有前缀0). 要求从高位到低位,进行 进栈出栈 操作,是最后输出的结果最大. 输入 ...
- [luoguP2827] 蚯蚓(堆?队列!)
传送门 35分做法 用堆来取最大值,暴力更新其余数的值. 65~85分做法 还是用堆来取最大值,其余的数增加可以变成新切开的两个数减少,最后统一加上一个数. #include <queue> ...
- Linux find常用命令
今天研究一下find的一些常用的命令. find格式:find filepath [-option] [-print|-exec|-ok...] 其中常用的option主要有 -type d|f|s| ...
- Python基础之 一 字典(dict)
字典:是一种key - value的数据类型.语法:info = { key:value }特性:无序,key必须唯一(所以天生去重) 方法如下:del dict[key]:删除字典指定键len(di ...
- [bzoj1324]Exca王者之剑_最小割
Exca王者之剑 bzoj-1324 题目大意:题目链接. 注释:略. 想法: 最小割经典模型. 所有格子向源点连权值为格子权值的边. 将棋盘黑白染色后白点反转源汇. 如果两个格子相邻那么黑点向白点连 ...