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

8
aaabcabc
3
1 3
3 8
4 8

Sample Output

1
3
5

HINT

 

Source

[Submit][Status][Discuss]

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的更多相关文章

  1. 【BZOJ2795】[Poi2012]A Horrible Poem hash

    [BZOJ2795][Poi2012]A Horrible Poem Description 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串 ...

  2. 2795: [Poi2012]A Horrible Poem

    2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 484  Solved: 235[Subm ...

  3. BZOJ 2795: [Poi2012]A Horrible Poem( hash )

    ...字符串hash. 假如长度x是一个循环节, 那么对于任意n(x | n)也是一个循环节. 设当前询问区间[l, r]长度为len = ∏piai, 最终答案ans = ∏piai' ,我们只需枚 ...

  4. P3538 [POI2012]OKR-A Horrible Poem

    P3538 [POI2012]OKR-A Horrible Poem hash+线性筛 题解 <----这篇写的不错(其实是我懒得码字了qwq) UVA10298 Power Strings 的 ...

  5. bzoj 2795 [Poi2012]A Horrible Poem hash+数论

    2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 640  Solved: 322[Subm ...

  6. 洛谷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 ...

  7. BZOJ2795/2890/3647 [Poi2012]A Horrible Poem 【字符串hash】

    题目链接 BZOJ2795 BZOJ2890 BZOJ3647 题解 三倍经验! 我们要快速求区间最小循环节 我们知道循环节有如下性质: ①当\(L\)为循环节长度,那么\(s[l...r - L] ...

  8. [Poi2012]A Horrible Poem BZOJ2795

    分析: 这是今天下午的考试题,推了2个小时,考试中A掉了 首先,循环串通过字符串hash可以O(1)判断:get_hash(l,r-len)==get_hash(l+len,r);显然可证. 我们其次 ...

  9. BZOJ2795&2890&3647[Poi2012]A Horrible Poem——hash

    题目描述 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. 输入 第一行一个正整数n (n<= ...

随机推荐

  1. Python 写Windows Service服务程序

    1.需求 为什么要开发一个windows服务呢?之前做一个程序,必须要读取指定目录文件License, 因为其他程序也在读取这指定目录的License文件,且License不同时会修改License的 ...

  2. 【JAVA 文件概述】

    一.概述 使用此类的原因: 该类将文件或者文件夹封装成对象.方便对文件与文件夹的属性信息进行操作.File对象作为参数传递给流的构造函数.要求:使用File类的常用方法. windows平台下,目录分 ...

  3. SQLAlchemy ORM高级查询之过滤,排序

    order_by,filter的语法. 用久了才会熟悉. Session = sessionmaker(bind=engine) session = Session() print(session.q ...

  4. 【131031】asp.net <%%>&<%#%>&<%=%>&<%@%>&<%$%>用法区别

    1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...

  5. html5 web database

    html5 web database <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  6. 编译报错dereferencing pointer to incomplete type

    关于编译报错“dereferencing pointer to incomplete type... 多是没找到结构体的定义,可以在本地复制其定义试试. 参考: http://my.oschina.n ...

  7. XSS攻击&SQL注入攻击&CSRF攻击?

    - XSS(Cross Site Script,跨站脚本攻击)是向网页中注入恶意脚本在用户浏览网页时在用户浏览器中执行恶意脚本的攻击方式.跨站脚本攻击分有两种形式:反射型攻击(诱使用户点击一个嵌入恶意 ...

  8. Python入门之树莓派

    Linux命令行$+命令 pwd显示当前目录 ls列表 cd改变当前目录,/ sudo超级用户输入,特权来操作系统相关设置或删除文件 sudo apt-get  install  安装程序 sudo ...

  9. Angular JS 学习之控制器

    1.AngularJS控制器 控制AngularJS的应用程序的数据:AngularJS控制器是常规的javaScript对象: 2.AngularJS应用程序被控制器控制,ng-controller ...

  10. DSP using MATLAB 示例Example2.4

    n = [0:10]; x = stepseq(0,0,10) - stepseq(10,0,10); [xe,xo,m] = evenodd(x,n); set(gcf,'Color',[1,1,1 ...