题目链接

BZOJ2795

BZOJ2890

BZOJ3647

题解

三倍经验!

我们要快速求区间最小循环节

我们知道循环节有如下性质:

①当\(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】的更多相关文章

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

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

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

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

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

  4. [BZOJ2795][Poi2012]A Horrible Poem

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

  5. 2795: [Poi2012]A Horrible Poem

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

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

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

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

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

  8. P3538 [POI2012]OKR-A Horrible Poem

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

  9. [Poi2012]A Horrible Poem BZOJ2795

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

随机推荐

  1. Ruby基础教程 1-10

    类结构 1.数值类结构     Fixnum到Bignum会自动转换   2.常用数值表示   3. ans=10.divmod(3) ans[0]是商  ans[1]是余数   4.实例方法roun ...

  2. 使用materialization

    explain select `countries`.`id` AS `id`,`countries`.`sortname` AS `sortname`,`countries`.`name` AS ` ...

  3. C# 委托 / 跨线程访问UI / 线程间操作无效: 从不是创建控件“Form1”的线程访问它

    C# 委托 / 跨线程访问UI /  线程间操作无效: 从不是创建控件“Form1”的线程访问它 网上的代码都比较复杂,还是这个简单 见代码, 简易解决办法: 主窗体代码 using System; ...

  4. 180709-Java实现获取本机Ip的工具类

    180709-Java实现获取本机Ip的工具类 获取本机Ip算是比较常见的一个需求场景了,比如业务报警,可能就会带上出问题的机器IP,方便直接上去看日志定位问题,那么问题来了,如何获取机器IP呢? I ...

  5. Linux命令应用大词典-第11章 Shell编程

    11.1 declare:显示或设置Shell变量 11.2 export:显示或设置环境变量 11.3 set:显示和设置Shell变量 11.4 unset:删除变量或函数 11.5 env:查看 ...

  6. 关于axios跨域带cookie

    axios  设置 withCredentials :true $u = $_SERVER['HTTP_REFERER'];$u = preg_replace('#/$#', '', $u);head ...

  7. JAVA基础学习之路(八)[1]String类的基本特点

    String类的两种定义方式: 直接赋值 通过构造方法赋值 //直接赋值 public class test2 { public static void main(String args[]) { S ...

  8. redis 面试

    Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog.G ...

  9. Python replace方法并不改变原字符串

    直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...

  10. HDU 1232 并查集板子题

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...