[BZOJ2795] [Poi2012] [字符串hash] A Horrible Poem
来源:互联网 发布:如何优化标题 编辑:程序博客网 时间:2024/05/20 12:22
一个字符串的最小周期等于这个字符串的长度-border的长度
那么如果x是字符串的周期,[l,r-x]一定和[l+x,r]相等。
然后发现x一定是字符串的长度的约数,就枚举一下约数,字符串hash判一判就可以了
线性筛预处理一下可以O(log)质因数分解
#include <cstdio>#include <iostream>#include <algorithm>#define N 500010#define base 139using namespace std;typedef unsigned long long ll;int n,q;int p[N],divi[N];char a[N];ll hash[N],power[N];inline void Pre(){ for(int i=2;i<=N-10;i++){ if(!p[i]) p[++*p]=i,divi[i]=i; for(int j=1;j<=*p&&1ll*p[j]*i<=N-10;j++){ p[p[j]*i]=1; divi[p[j]*i]=p[j]; if(i%p[j]==0) break; } }}inline bool check(int l,int r,int L,int R){ ll h1=hash[r]-hash[l-1]*power[r-l+1],h2=hash[R]-hash[L-1]*power[R-L+1]; return h1==h2;}inline int query(int l,int r){ int len=r-l+1,ret=r-l+1; while(len^1){ int cur=divi[len]; while(ret%cur==0&&check(l,r-ret/cur,l+ret/cur,r)) ret/=cur; while(len%cur==0) len/=cur; } return ret;}int main(){ //freopen("1.in","r",stdin); //freopen("1.out","w",stdout); scanf("%d%s%d",&n,a+1,&q); power[0]=1; for(int i=1;i<=n;i++) hash[i]=hash[i-1]*base+a[i]-'a'+1,power[i]=power[i-1]*base; Pre(); while(q--){ int l,r; scanf("%d%d",&l,&r); printf("%d\n",query(l,r)); } return 0;}
阅读全文
0 0
- 字符串hash,bzoj2795[Poi2012]A Horrible Poem
- [BZOJ2795] [Poi2012] [字符串hash] A Horrible Poem
- 【bzoj2795】[Poi2012]A Horrible Poem hash
- bzoj2795[Poi2012]A Horrible Poem 暴力hash
- 【POI2012】【BZOJ2795】A Horrible Poem
- BZOJ2795: [Poi2012]A Horrible Poem
- BZOJ2795 [Poi2012]A Horrible Poem
- bzoj2795: [Poi2012]A Horrible Poem
- BZOJ2795/POI2012 A horrible poem
- BZOJ2795: [Poi2012]A Horrible Poem
- 【BZOJ2795】[Poi2012]A Horrible Poem【Hash】【GCD】【暴力】
- BZOJ 2795 Poi2012 A Horrible Poem Hash
- 2795: [Poi2012]A Horrible Poem hash
- bzoj 2795 [Poi2012]A Horrible Poem hash
- bzoj 2795: [Poi2012]A Horrible Poem hash
- 2795: [Poi2012]A Horrible Poem
- [POI 2012]A Horrible Poem(字符串Hash)
- [BZOJ 2795]POI2012 A Horrible Poem
- API 开发的心得总结
- php自带压缩方法的性能比较
- 自定义可滑动的按钮
- 淘淘商城系列——maven工程debug调试
- cxf环境搭建与第一个项目
- [BZOJ2795] [Poi2012] [字符串hash] A Horrible Poem
- 在orangePi上启动MAVProxy的方法
- Codeforces Round #416 (Div. 2) ABC
- JSON的序列化与反序列化及其在前后台交互中的应用
- 找到一个Delphi xe帮助文档剥离的方法
- Java笔记之final关键字
- linux shell (历史命令)
- Multithreaded simple data type access and atomic variables
- c# winform结合数据库动态生成treeview的父节点和子节点方法和思路