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);显然可证. 我们其次 ...
随机推荐
- Python 列表下标操作
Python 列表下标操作 引用网址: https://www.jianshu.com/p/a98e935e4d46
- C if语句判断年龄
#include <stdio.h> int main(int argc, char **argv) { //新建两个变量给变量赋值跟初始化:const int a=45;int c=0; ...
- 【button】 按钮组件说明
原型: <button size="[default | mini]" type="[primary | default | warn]" plain=& ...
- spark集群安装部署
通过Ambari(HDP)或者Cloudera Management (CDH)等集群管理服务安装和部署在此不多介绍,只需要在界面直接操作和配置即可,本文主要通过原生安装,熟悉安装配置流程. 1.选取 ...
- php多进程单例模式下的 MySQL及Redis连接错误修复
前几天写了个php常驻脚本,主要逻辑如下 //跑完数据后休息60秒 $sleepTime = 60; $maxWorker = 10; while (true) { $htmlModel = new ...
- python读取日志,存入mysql
1.从 http://www.almhuette-raith.at/apache-log/access.log 下载 1万条日志记录,保存为一个文件,读取文件并解析日志,从日志中提取ip, time_ ...
- MR execution in YARN
Overview YARN provides API not for application developers but for the great developers working on ne ...
- Python+Opencv实现把图片转为视频
1. 安装Opencv包 在Python命令行输入如下命令(如果你使用的Anaconda,直接进入Anaconda Prompt键入命令即可.如果你不知道Anaconda是什么,可以参考王树义老师的文 ...
- c# winform 服务器提交了协议冲突. Section=ResponseStatusLine
[转] 最近在用.net写一个网络蜘蛛,发现对有的网站用HttpWebrequest抓取网页的时候会报错,捕获异常提示:"服务器提交了协议冲突 Section=ResponseStatusL ...
- 条款02:尽量以const,enum,inline替换#define
一.概述 尽量少用预处理器——宏替换 二.细节 1. 关于宏替换之常量 旧版本:#define N 10; 新版本:const int n = 10; 比较:#define不被视为语言的一部分,记号名 ...