BZOJ2795/2890/3647 [Poi2012]A Horrible Poem 【字符串hash】
题目链接
题解
三倍经验!
我们要快速求区间最小循环节
我们知道循环节有如下性质:
①当\(L\)为循环节长度,那么\(s[l...r - L] = s[l + L...r]\)且\(L | (r - l + 1)\)
②如果\(L\)为循环节,那么\(L x\)也为循环节
一个比较暴力的思想是枚举\(len = r - l + 1\)的因子,用\(hash\)去判是否相同,这样做是\(O(q\sqrt{n})\)的,过于暴力
由性质②我们知道,最后的答案\(L\)一定是\(len\)删除若干个质因子的结果,所以我们只需枚举质因子即可
由于质因子个数是\(O(logn)\)的,所以预处理一下即可\(O(logn)\),枚举质因子
复杂度\(O(qlogn)\)
要注意,\(P3647\)卡自然溢出
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define ULL unsigned long long int
using namespace std;
const int maxn = 500005,maxm = 100005,INF = 1000000000,P = 2001611;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
ULL pw[maxn],h[maxn];
LL pw2[maxn],h2[maxn];
char s[maxn];
int n,q,fac[maxn],p[maxn],pi,isn[maxn];
void init(){
for (int i = 2; i <= n; i++){
if (!isn[i]) p[++pi] = i,fac[i] = i;
for (int j = 1; j <= pi && i * p[j] <= n; j++){
isn[i * p[j]] = true; fac[i * p[j]] = p[j];
if (i % p[j] == 0) break;
}
}
}
inline bool check(int l,int r,int ll,int rr){
return h[r] - h[l - 1] * pw[r - l + 1] == h[rr] - h[ll - 1] * pw[rr - ll + 1]
&& ((h2[r] - h2[l - 1] * pw2[r - l + 1] % P) % P + P) % P == ((h2[rr] - h2[ll - 1] * pw2[rr - ll + 1] % P) % P + P) % P;
}
int main(){
n = read();
pw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * 107;
pw2[0] = 1; for (int i = 1; i <= n; i++) pw2[i] = pw2[i - 1] * 107 % P;
scanf("%s",s + 1);
init();
for (int i = 1; i <= n; i++){
h[i] = h[i - 1] * 107 + s[i];
h2[i] = (h2[i - 1] * 107 % P + s[i]) % P;
}
q = read();
int l,r,len,ans;
while (q--){
l = read(); r = read(); len = r - l + 1;
ans = len;
for (int x = len; x > 1; x /= fac[x]){
int L = ans / fac[x];
if (check(l,r - L,l + L,r))
ans = L;
}
printf("%d\n",ans);
}
return 0;
}
BZOJ2795/2890/3647 [Poi2012]A Horrible Poem 【字符串hash】的更多相关文章
- BZOJ2795&2890&3647[Poi2012]A Horrible Poem——hash
题目描述 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. 输入 第一行一个正整数n (n<= ...
- 洛谷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】[Poi2012]A Horrible Poem hash
[BZOJ2795][Poi2012]A Horrible Poem Description 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串 ...
- [BZOJ2795][Poi2012]A Horrible Poem
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 261 Solved: 150[Subm ...
- 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' ,我们只需枚 ...
- 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
P3538 [POI2012]OKR-A Horrible Poem hash+线性筛 题解 <----这篇写的不错(其实是我懒得码字了qwq) UVA10298 Power Strings 的 ...
- [Poi2012]A Horrible Poem BZOJ2795
分析: 这是今天下午的考试题,推了2个小时,考试中A掉了 首先,循环串通过字符串hash可以O(1)判断:get_hash(l,r-len)==get_hash(l+len,r);显然可证. 我们其次 ...
随机推荐
- unity share current game screen
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.IO; public class Tak ...
- 「国庆训练」ArcSoft's Office Rearrangement(HDU-5933)
题目与分析 题解见https://blog.csdn.net/cmershen/article/details/53200922. 训练赛场上我们写出来了--在4小时50分钟的时候...激情补题啊.. ...
- selenium,unittest——参数化url,并多线程加快脚本运行速度
利用参数化连续打开网页: #encoding=utf-8import unittestimport paramunittestimport timefrom selenium import webdr ...
- Unity - Humanoid设置Bip骨骼导入报错
报错如下: 解决: 原因是biped骨骼必须按照Unity humanoid的要求设置,在max中设置如下:
- javac 编译过程
javac 编译过程 一.解析与填充符号表: 1. 语法.此法分析: a) 语法分析:将源代码字符流转换为标记(Token:编译过程最小元素)集合. b) ...
- hadoop3.0新特性及新功能
Hadoop-3.0.0-alpha2版本发布,相比之前的hadoop-2.x有一系列的功能增强.但目前还是个alpha版本,有很多bug,且不能保证API的稳定和质量. 主要变化 Java最低版本要 ...
- 收割大厂offer需要具备的条件
转载出处 本人也一直在关注互联网,觉得还是有些了解.互联网要求是越来越高了,竞争的人太多了,不过你不用担心,个人觉得,你到了中层的水平,拿二线offer应该没问题,人多也有人多的好处,我比别人多努力一 ...
- 软件功能-东北师大站-第三次作业(PSP)
1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 本周PSP饼状图
- Notes of the scrum meeting before publishing2(12.18)
meeting time:18:30~20:30p.m.,December 18th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- 百度编辑器ueditor的图片地址修正
我用的百度编辑器为1.4.2的,相对于现在这个时间来说是比较新的.之前去的1.3版的,后来更新到1.4之后出现路径问题.因为今天晚上出现特别奇怪的问题,所以特地又整了一遍,发现这玩意还是得自己弄通了好 ...