[BZOJ2795][Poi2012]A Horrible Poem
2795: [Poi2012]A Horrible Poem
Time Limit: 50 Sec Memory Limit: 128 MB
Submit: 261 Solved: 150
[Submit][Status][Discuss]
Description
给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节。
如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到。
Input
第一行一个正整数n (n<=500,000),表示S的长度。
第二行n个小写英文字母,表示字符串S。
第三行一个正整数q (q<=2,000,000),表示询问个数。
下面q行每行两个正整数a,b (1<=a<=b<=n),表示询问字符串S[a..b]的最短循环节长度。
Output
依次输出q行正整数,第i行的正整数对应第i个询问的答案。
Sample Input
aaabcabc
3
1 3
3 8
4 8
Sample Output
3
5
HINT
Source
hash一下,然后有一个显然的暴力做法,单次询问是$\sqrt n$的。
可以优化一下,记录一下每个字母的出现次数,循环节个数一定是这个的约数。
时间复杂度不是很好$O(n+q+q\sqrt n)$
#include<cstdio>
#include<algorithm>
#define N 500050
#define ll unsigned long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
ll pow[N]={},hash[N];
int n,num[N][],q,st[N],top;
char s[N];
inline ll gethash(int l,int r)
{return hash[r]-hash[l-]*pow[r-l+];}
inline bool check(int l,int r,int x)
{return gethash(l+x,r)==gethash(l,r-x);}
inline int gcd(int a,int b){return b?gcd(b,a%b):a;}
inline int query(int l,int r)
{
swap(l,r);
int x=r-l+,y=r-l+;
for(int i=;i<=;i++)
x=gcd(x,num[r][i]-num[l-][i]);
if(x==)return y;
top=;
for(int i=;i*i<=x;i++)
if(!(x%i))
{
st[++top]=y/i;
if(check(l,r,y/x*i))
return y/x*i;
}
while(!check(l,r,st[top]))top--;
return st[top];
}
int main()
{
scanf("%d\n",&n);
gets(s+);
for(int i=;i<=n;i++)
hash[i]=hash[i-]*+s[i],
pow[i]=pow[i-]*;
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
num[i][j]=num[i-][j]+(s[i]-'a'+==j);
q=read();
while(q--)printf("%d\n",query(read(),read()));
}
[BZOJ2795][Poi2012]A Horrible Poem的更多相关文章
- 【BZOJ2795】[Poi2012]A Horrible Poem hash
[BZOJ2795][Poi2012]A Horrible Poem Description 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串 ...
- 2795: [Poi2012]A Horrible Poem
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 484 Solved: 235[Subm ...
- BZOJ 2795: [Poi2012]A Horrible Poem( hash )
...字符串hash. 假如长度x是一个循环节, 那么对于任意n(x | n)也是一个循环节. 设当前询问区间[l, r]长度为len = ∏piai, 最终答案ans = ∏piai' ,我们只需枚 ...
- P3538 [POI2012]OKR-A Horrible Poem
P3538 [POI2012]OKR-A Horrible Poem hash+线性筛 题解 <----这篇写的不错(其实是我懒得码字了qwq) UVA10298 Power Strings 的 ...
- bzoj 2795 [Poi2012]A Horrible Poem hash+数论
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 640 Solved: 322[Subm ...
- 洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]
题目传送门 A Horrible Poem 题目描述 Bytie boy has to learn a fragment of a certain poem by heart. The poem, f ...
- BZOJ2795/2890/3647 [Poi2012]A Horrible Poem 【字符串hash】
题目链接 BZOJ2795 BZOJ2890 BZOJ3647 题解 三倍经验! 我们要快速求区间最小循环节 我们知道循环节有如下性质: ①当\(L\)为循环节长度,那么\(s[l...r - L] ...
- [Poi2012]A Horrible Poem BZOJ2795
分析: 这是今天下午的考试题,推了2个小时,考试中A掉了 首先,循环串通过字符串hash可以O(1)判断:get_hash(l,r-len)==get_hash(l+len,r);显然可证. 我们其次 ...
- BZOJ2795&2890&3647[Poi2012]A Horrible Poem——hash
题目描述 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. 输入 第一行一个正整数n (n<= ...
随机推荐
- GPT vs MBR 分区 ,,, Legacy BIOS vs UEFI BIOS
MBR与GPT两种磁盘分区格式的区别 http://itoedr.blog.163.com/blog/static/120284297201378114053240 GPT Partition Tab ...
- Ext.MessageBox消息框
Ext JS消息提示框主要包括:alert.confirm.prompt.show 1.Ext.MessageBox.alert() 调用格式: alert( String title, String ...
- 使用windows的远程桌面连接连接Ubuntu
想起来用笔记本连接一个windows server时只需要在远程桌面连接里面输入一下ip地址然后账号密码就可以了,十分简单.于是乎既然装了个Ubuntu当服务器使那么我就业来远程连接一下,由于wind ...
- POJ 3241 Object Clustering 曼哈顿最小生成树
Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...
- loj 1316(spfa预处理+状压dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27024 题意:求0-(n-1)的经过最多的标记的点的最短路. 思路 ...
- 关于如何在MFC工程中输入不同的数据进行调试
我们可以采用c++的文件输入输出来进行调试 这样就绕过了不能使用黑窗口输入数据就不能调试的思维定式 不是黑窗口的我们都可以考虑用文件流输入输出 或者用控件来输入? http://blog.csdn.n ...
- 生成n位随机字符串
--1.借助newid() Go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...
- 近实时运算的利器---presto在公司实践
1.起因 公司hadoop集群里的datanonde和tasktracker节点负载主要集中于晚上到凌晨,平日工作时间负载不是很高.但在工作时间内,公司业务人员有实时查询需求,现在主要 借助于hive ...
- 时间模块。。time
- virtualtree 的使用(Delphi)
VirtualTreeview的强大,毋庸置疑,不过,你能给演示演示,也不错,就是刚下来,只有一个可执行程序,感觉像病毒. 最近比较忙,没有上网,现在把我研究的结果和大家通报下,方便新手学习,避免走弯 ...