A Horrible Poem (字符串hash+数论)
# 10038. 「一本通 2.1 练习 4」A Horrible Poem
【题目描述】
给出一个由小写英文字母组成的字符串 $S$,再给出 $q$ 个询问,要求回答 $S$ 某个子串的最短循环节。
如果字符串 $B$ 是字符串 $A$ 的循环节,那么 $A$ 可以由 $B$ 重复若干次得到。
【算法】
-首先对于长度为 $len$ 的子串,循环节长度为 $x$ 的充要条件:$[1,len-x]$串的哈希值等于 $[x+1,len]$ 串的哈希值。
-一开始暴力枚举最短循环节长度(n的约数),然后T了一半。
有两种方法进行优化:
1、循环节个数必然是子串各个字母个数和子串长度的公约数,可以枚举循环节个数此时bzoj就能过了,但是loj还是会T。(93分) $O(q\sqrt{n})$
2、正解:假设最短循环节长度为len则原串长度显然为len*k。若只考虑k,并且将k的质因数依次分解,每次试除k,则得到的$k^。$和len的乘积仍是循环节,利用这个性质。依次用质因数 $i$ 试除n,若除去后仍是循环节,说明i属于k,将其除去,结果就留下了len。
【代码1】
#include <bits/stdc++.h>
#define P 131
#define ULL unsigned long long
using namespace std;
int n,q,a,b,len;
int rec[500010][26];
ULL h[500010],p[500010];
char s[500010];
inline int read() {
int x=0,f=1; char c=getchar();
while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); }
while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); }
return x*f;
}
void parse() {
p[0]=1;
for(int i=1;i<=n;i++) p[i]=p[i-1]*P,h[i]=h[i-1]*P+(ULL)s[i];
for(int i=1;i<=n;i++)
for(int j=1;j<=26;j++)
rec[i][j]=rec[i-1][j]+(s[i]-'a'+1==j);
}
int gcd(int x,int y) {
return y?gcd(y,x%y):x;
}
bool valid(int l) {
return h[b]-h[a+l-1]*p[len-l]==h[a+(len/l-1)*l-1]-h[a-1]*p[len-l];
}
int main() {
n=read();
gets(s+1);
q=read();
parse();
while(q--) {
a=read(),b=read();
len=b-a+1;
int num=len,ans=0;
for(int i=1;i<=26;i++) num=gcd(num,rec[b][i]-rec[a-1][i]);
for(int i=1;i*i<=num;i++) {
if(num%i==0) {
if(valid(len/(num/i))) { ans=max(ans,num/i); break; }
if(valid(len/i)) ans=max(ans,i);
}
}
printf("%d\n",len/ans);
}
return 0;
}
【ac代码】
#include <bits/stdc++.h>
#define N 500010
#define P 131
#define ULL unsigned long long
using namespace std;
int n,q,len,tot;
ULL h[N],p[N];
int prime[N],minp[N];
char s[N];
inline int read() {
int x=0,f=1; char c=getchar();
while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); }
while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); }
return x*f;
}
void parse() {
for(int i=2;i<=n;i++) {
if(!minp[i]) {
prime[++tot]=i;
minp[i]=i;
}
for(int j=1;j<=tot;j++) {
if(prime[j]>minp[i]||prime[j]*i>n) break;
minp[prime[j]*i]=prime[j];
}
}
p[0]=1;
for(int i=1;i<=n;i++)
h[i]=h[i-1]*P+(ULL)s[i],p[i]=p[i-1]*P;
}
bool valid(int a,int b,int l) {
return h[b]-h[a+l-1]*p[len-l]==h[a+(len/l-1)*l-1]-h[a-1]*p[len-l];
}
int main() {
n=read();
gets(s+1);
q=read();
parse();
while(q--) {
int a,b,ans,tmp;
a=read(),b=read();
len=tmp=ans=b-a+1;
while(tmp!=1) {
int t=minp[tmp];
while(tmp%t==0&&valid(a,b,ans/minp[tmp])) tmp/=t,ans/=t;
while(tmp%t==0) tmp/=t;
}
printf("%d\n",ans);
}
return 0;
}
A Horrible Poem (字符串hash+数论)的更多相关文章
- 洛谷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 ...
- 【hash】A Horrible Poem
[题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...
- bzoj 2795 [Poi2012]A Horrible Poem hash+数论
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 640 Solved: 322[Subm ...
- BZOJ 2795: [Poi2012]A Horrible Poem( hash )
...字符串hash. 假如长度x是一个循环节, 那么对于任意n(x | n)也是一个循环节. 设当前询问区间[l, r]长度为len = ∏piai, 最终答案ans = ∏piai' ,我们只需枚 ...
- 【BZOJ2795】[Poi2012]A Horrible Poem hash
[BZOJ2795][Poi2012]A Horrible Poem Description 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串 ...
- 2795: [Poi2012]A Horrible Poem
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 484 Solved: 235[Subm ...
- [BZOJ2795][Poi2012]A Horrible Poem
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 261 Solved: 150[Subm ...
- #10038.A Horrible Poem
#10038.A Horrible Poem 题目传送门 思路解析 既然这道题目在hash板块里,那么自然就可以想到用hash做这道题目. 首先我们可以用hash数组存储字符串的前缀的hash值. 因 ...
- P3538 [POI2012]OKR-A Horrible Poem
P3538 [POI2012]OKR-A Horrible Poem hash+线性筛 题解 <----这篇写的不错(其实是我懒得码字了qwq) UVA10298 Power Strings 的 ...
随机推荐
- Vue_(组件通讯)动态组件
动态组件 传送门 在一个元素上挂载多个组件,根据不同状态进行切换的时候,可以使用动态组件 动态组件的使用:需要使用内置组件<component></component>,根据 ...
- exgcd 解同余方程ax=b(%n)
ax=n(%b) -> ax+by=n 方程有解当且仅当 gcd(a,b) | n ( n是gcd(a,b)的倍数 ) exgcd解得 a*x0+b*y0=gcd(a,b) 记k=n/gc ...
- 开源!js实现微信/QQ直接跳转到支付宝APP打开口令领红包!附:demo
最近支付宝的领红包可真是刷爆了各个微信群啊,满群都是支付宝口令. 可是这样推广可不是办法,又要复制又要打开支付宝又要点领取,太麻烦了. 于是乎,提出了一个疑问!是否可以在微信里面点一个链接然后直接打开 ...
- 深入聚焦 call,apply 和 bind
在JavaScript 中,call.apply 和 bind 是 Function 对象自带的三个方法,这三个方法的主要作用是改变函数中的 this 指向,从而可以达到`接花移木`的效果.本文将对这 ...
- 在已开启Chrome窗口上调试
代码 @Test void testNow() { /* First: Add the chrome.exe to the PATH. * Then: open the cmd and input t ...
- 转 Golang 入门 : 切片(slice)
https://www.jianshu.com/p/354fce23b4f0 切片(slice)是 Golang 中一种比较特殊的数据结构,这种数据结构更便于使用和管理数据集合.切片是围绕动态数组的概 ...
- leetcode1282 用户分组
class Solution { public: vector<vector<int>> groupThePeople(vector<int>& group ...
- LC 655. Print Binary Tree
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...
- Python:目录
ylbtech-Python:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtec ...
- vue画图运用echarts
<template> <div class="tubiao"> <div id="main" style="width: ...