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.x Day6 socketserver
socketserver是啥? 简化了编写网络服务器,就是对于socket的再一次封装sockerserver包含4个类可以使用:A=socketserver.TCPServer() #用于TCP/I ...
- centOS7+mariadb+Nginx+PHP7.0 安装
1.前期准备工作 更新 yum 源,自带的源没有 PHP5.6 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7 ...
- echarts的简单应用之(一)柱形图
前段时间做项目需要绘制一些图表来展示信息,从网上资料来看用的比较多的是百度开源的echarts.echarts的官网上有API以及demo.上面的图形都是非常炫的,不过都是静态 数据,我们做项目时不可 ...
- 远程调试nodejs
一 windows作为远程服务器 1.在远程服务器(192.168.1.1)上安装node-inspector:npm install -g node-inspector // -g 导入安装路径 ...
- expdp,impdp,include,exclude
The examples below are based on:- the demo schema SCOTT that is created with script: $ORACLE_HOME/rd ...
- Chrome & CORS & Fetch API & Chrome 多开,应用分身
Chrome & CORS & Fetch API Chrome 浏览器的跨域设置 https://www.cnblogs.com/cshi/p/5660039.html https: ...
- hdu 361B
#include<stdio.h> int a[100100]; int main() { int n,i,k; while(scanf("%d%d",&n,& ...
- Python基础之 一 字典(dict)
字典:是一种key - value的数据类型.语法:info = { key:value }特性:无序,key必须唯一(所以天生去重) 方法如下:del dict[key]:删除字典指定键len(di ...
- 常用的delphi 第三方控件
Devexpress VCL 这个基本上覆盖了系统界面及数据库展示的方方面面,是做桌面系统必备的一套控件,目前的版本是2011.2.3, 支持win32 及win64. AutoUpgrader 这个 ...
- 洛谷——P2866 [USACO06NOV]糟糕的一天Bad Hair Day
https://www.luogu.org/problem/show?pid=2866 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are h ...