题目传送门

A Horrible Poem

题目描述

Bytie boy has to learn a fragment of a certain poem by heart. The poem, following the best lines of modern art, is a long string consisting of lowercase English alphabet letters only. Obviously, it sounds horrible, but that is the least of Bytie's worries.

First and foremost, he completely forgot which fragment is he supposed to learn. And they all look quite difficult to memorize...

There is hope, however: some parts of the poem exhibit certain regularity. In particular, every now and then a fragment, say , is but a multiple repetition of another fragment, say  (in other words, , i.e., , where  is an integer). In such case we say that  is a full period of  (in particular, every string is its own full period).

If a given fragment has a short full period, Bytie's task will be easy. The question remains... which fragment was that?

Make a gift for Bytie - write a program that will read the whole poem as well as a list of fragments that Bytie suspects might be the one he is supposed to memorize, and for each of them determines its shortest full period.

给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节。

如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到。

输入输出格式

输入格式:

In the first line of the standard input there is a single integer  ().

In the second line there is a string of length  consisting of lowercase English alphabet letters-the poem.

We number the positions of its successive characters from 1 to .

The next line holds a single integer  () denoting the number of fragments.

Then the following  lines give queries, one per line.

Each query is a pair of integers  and  (), separated by a single space, such that the answer to the query should be the length of the shortest full period of the poem's fragment that begins at position  and ends at position .

In tests worth in total 42% of the points  holds in addition.

In some of those, worth 30% of points in total,  holds as well.

输出格式:

Your program should print  lines on the standard output.

The -th of these lines should hold a single integer - the answer to the -th query.

输入输出样例

输入样例#1:

8
aaabcabc
3
1 3
3 8
4 8
输出样例#1:

1
3
5

说明

给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节。

如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到。


  分析:

  一道稍考思维的字符串题目,一开始没想到第三条性质只能打出暴力。

  本道题共有三个重要的性质,只要能想到也就不难写出来了:

  1,循环节长度一定是区间长度的约数

  2,如果n是一个循环节长度,那么k*n(当然k*n也要是区间长度的约数)也会是一个循环节长度

  3,满足一个子串为该段字符串区间的循环节的充要条件是$[l,r-t]$与$[l+t,r]$的$hash$值相同,其中$t$是该子串的长度

  有了上面三条性质也就不难做了。

  首先预处理出$n$范围内的质因数,然后从大到小枚举区间长度的质因数,用性质3计算该长度的子串是不是循环节即可。当然,预处理需要用优化版的埃氏筛,否则会被愉快地T飞。。。

  Code:

//It is made by HolseLee on 10th Aug 2018
//Luogu.org P3538
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#pragma GCC optimize(2)
using namespace std; typedef unsigned long long ull;
const int N=5e5+;
const ull base=;
ull h[N],a[N];
int n,m,nxt[N],q[N<<];
char s[N];
bool vis[N]; inline int read()
{
char ch=getchar();int num=;
while(ch<''||ch>'')ch=getchar();
while(ch>=''&&ch<=''){
num=num*+ch-'';ch=getchar();
}
return num;
} inline void print(int x)
{
if(x>)print(x/);
putchar(x%+'');
} void divisor()
{
int top=;
for(int i=;i<N;++i){
if(vis[i]){
for(int j=;j<=top&&(ull)(i*q[j])<N;++j){
nxt[i*q[j]]=q[j],
vis[i*q[j]]=true;
if(i%q[j]==)break;
}
}
else {
vis[q[++top]=i]=true;nxt[i]=i;
for(int j=;j<=top&&(ull)(i*q[j])<N;++j){
nxt[i*q[j]]=q[j],
vis[i*q[j]]=true;
}
}
}
} inline bool check(int l1,int r1,int l2,int r2)
{
return ((h[r1]-h[l1-]*a[r1-l1+])==(h[r2]-h[l2-]*a[r2-l2+]));
} int main()
{
n=read();
scanf("%s",s+);
for(int i=;i<=n;++i){
h[i]=h[i-]*base+s[i]-'a'+;
}
a[]=;
for(int i=;i<=n;++i){
a[i]=a[i-]*base;
}
divisor();
m=read();
int l,r,tot,len,t;
for(int i=;i<=m;++i){
l=read(),r=read();
len=r-l+;tot=;
while(len!=){
q[++tot]=nxt[len];
len/=nxt[len];
}
len=r-l+;
for(int j=;j<=tot;++j)
if(len>=q[j]){
t=len/q[j];
if(check(l,r-t,l+t,r)){
len=t;
}
}
print(len);putchar('\n');
}
return ;
}

洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]的更多相关文章

  1. 洛谷P3832 [NOI2017]蚯蚓排队 【链表 + 字符串hash】

    题目链接 洛谷P3832 题解 字符串哈希然后丢到hash表里边查询即可 因为\(k \le 50\),1.2操作就暴力维护一下 经复杂度分析会发现直接这样暴力维护是对的 一开始自然溢出WA了,还以为 ...

  2. 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告

    P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...

  3. 洛谷P3533 [POI2012]RAN-Rendezvous

    P3533 [POI2012]RAN-Rendezvous 题目描述 Byteasar is a ranger who works in the Arrow Cave - a famous rende ...

  4. BZOJ2801/洛谷P3544 [POI2012]BEZ-Minimalist Security(题目性质发掘+图的遍历+解不等式组)

    题面戳这 化下题面给的式子: \(z_u+z_v=p_u+p_v-b_{u,v}\) 发现\(p_u+p_v-b_{u,v}\)是确定的,所以只要确定了一个点\(i\)的权值\(x_i\),和它在同一 ...

  5. 【bzoj2795】【Poi2012】A Horrible Poem

    题解: 询问区间的整循环节 设区间长度为$n$ 如果有循环节长为$x$和$y$,那由斐蜀定理得$gcd(x,y)$也一定为一个循环节: 假设最小的循环节长为$mn$,那么对于任何循环节长$x$,一定$ ...

  6. 洛谷P3539 [POI2012] ROZ-Fibonacci Representation

    题目传送门 转载自:five20,转载请注明出处 本来看到这题,蒟蒻是真心没有把握的,还是five20大佬巨orz 首先由于斐波拉契数的前两项是1,1 ,所以易得对于任何整数必能写成多个斐波拉契数加减 ...

  7. 洛谷P3537 [POI2012]SZA-Cloakroom(背包)

    传送门 蠢了……还以为背包只能用来维护方案数呢……没想到背包这么神奇…… 我们用$dp[i]$表示当$c$的和为$i$时,所有的方案中使得最小的$b$最大时最小的$b$是多少 然后把所有的点按照$a$ ...

  8. 洛谷P3531 [POI2012]LIT-Letters

    题目描述 Little Johnny has a very long surname. Yet he is not the only such person in his milieu. As it ...

  9. 洛谷P3534 [POI2012] STU

    题目 二分好题 首先用二分找最小的绝对值差,对于每个a[i]都两个方向扫一遍,先都改成差满足的形式,然后再找a[k]等于0的情况,发现如果a[k]要变成0,则从他到左右两个方向上必会有两个连续的区间也 ...

随机推荐

  1. html中<meta>标签

    这个是html文档一般都有的元素. 1. 介绍 元素基本所有浏览器都支持,它提供页面的元信息,比如描述.关键字.web服务等 位于文档头部的内部,将以名称/值对出现 2. 属性 注意:如果没有name ...

  2. 树dp...吧 ZOJ 3949

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...

  3. 区间->点,点->区间,线段树优化建图+dijstra Codeforces Round #406 (Div. 2) D

    http://codeforces.com/contest/787/problem/D 题目大意:有n个点,三种有向边,这三种有向边一共加在一起有m个,然后起点是s,问,从s到所有点的最短路是多少? ...

  4. CF745 D 交互题 思维 二进制分解

    现有一矩阵你可以做出不超过20个询问 每个询问 要求输入列号,可以询问矩阵上每行上你给的列之中的最小值让你最后输出该矩阵每行的不包括对角线位置上的最小值考虑询问如何分组,考虑二分,以二进制位来分组 那 ...

  5. .NET面试题系列(一)基本概念

    什么是CLR CLR常用简写词语,CLR是公共语言运行库(Common Language Runtime)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集等),并保证应用和底 ...

  6. BZOJ做题记录[0512~?]

    觉得做一道开一篇真不好...好多想找的东西都被刷下去了... 至于?的日期究竟到什么时候...还是看心情...但是估计不会超过七天吧 最后更新时间:05/19 10:42 [05/14 10:56]我 ...

  7. js设置html区域隐藏和显示

    if(message != "指派") { document.getElementById("appoint").style.display="non ...

  8. 巧用margin/padding的百分比值实现高度自适应

    原文:https://segmentfault.com/a/1190000004231995 一个基础却又容易混淆的css知识点 本文依赖于一个基础却又容易混淆的css知识点:当margin/padd ...

  9. LintCode之二叉树的最大节点

    分治问题,可以把整棵树看做是由一颗颗只有三个节点组成的小树,一颗树的构成是根节点.左子树.右子树,这样只需要从左子树找出一个最大的节点,从右子树找出一个最大的节点,然后与根节点三个取个最大的,就是最终 ...

  10. E - Is It A Tree? 并查集判断是否为树

    题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...