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

...字符串hash.
假如长度x是一个循环节, 那么对于任意n(x | n)也是一个循环节.
设当前询问区间[l, r]长度为len = ∏piai, 最终答案ans = ∏piai' ,我们只需枚举len的质因数来确定ai'即可
#include<bits/stdc++.h> using namespace std; typedef unsigned long long ull; const int maxn = 500009;
const ull P = 1000173169; char S[maxn];
int check[maxn], prime[maxn], N = 0, n;
ull H[maxn], K[maxn]; void init() {
memset(check, 0, sizeof check);
for(int i = 2; i <= n; i++) {
if(!check[i])
check[i] = prime[N++] = i;
for(int j = 0; j < N && i * prime[j] <= n; j++) {
check[i * prime[j]] = prime[j];
if(i % prime[j] == 0) break;
}
}
H[n] = 0; K[0] = 1;
for(int i = n - 1; ~i; i--)
H[i] = H[i + 1] * P + S[i];
for(int i = 1; i <= n; i++)
K[i] = K[i - 1] * P;
} inline bool jud(int a, int b, int L) {
return H[a] - H[a + L] * K[L] == H[b] - H[b + L] * K[L];
} void Read() {
cin >> n;
char* t = S;
for(; !islower(*t); *t = getchar());
for(int i = 1; i < n; i++)
*++t = getchar();
} int main() { Read();
init(); int q; scanf("%d", &q);
while(q--) {
int l, r; scanf("%d%d", &l, &r); l--; r--;
int ans = r - l + 1, t = ans;
while(t > 1) {
int x = check[t];
while(ans % x == 0 && jud(l, l + ans / x, r - l + 1 - ans / x)) ans /= x;
for(; t % x == 0; t /= x);
}
printf("%d\n", ans);
} return 0;
}
2795: [Poi2012]A Horrible Poem
Time Limit: 50 Sec Memory Limit: 128 MB
Submit: 202 Solved: 115
[Submit][Status][Discuss]
Description
给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节。
如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到。
Input
第一行一个正整数n (n<=500,000),表示S的长度。
第二行n个小写英文字母,表示字符串S。
第三行一个正整数q (q<=2,000,000),表示询问个数。
下面q行每行两个正整数a,b (1<=a<=b<=n),表示询问字符串S[a..b]的最短循环节长度。
Output
依次输出q行正整数,第i行的正整数对应第i个询问的答案。
Sample Input
aaabcabc
3
1 3
3 8
4 8
Sample Output
3
5
HINT
Source
BZOJ 2795: [Poi2012]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+线性筛
题目大意 bzoj 2795 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节. 如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. n<=500 ...
- 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 hash
[BZOJ2795][Poi2012]A Horrible Poem Description 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串 ...
- 【BZOJ】2795: [Poi2012]A Horrible Poem
题意 一个长度为\(n(n \le 500000)\)的字符串\(s\),给\(q(q \le 2000000)\)个询问,每个询问给一个区间\([l, r]\),求这个区间内最短的循环节. 分析 分 ...
- [POI2012]OKR-A Horrible Poem hash
题面:洛谷 题解: 首先我们需要知道一个性质,串s的最小循环节 = len - next[len].其中next[len]表示串s的一个最长长度使得s[1] ~ s[next[len]] == s[l ...
- 【bzoj2795】[Poi2012]A Horrible Poem Hash+分解质因数
题目描述 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. 输入 第一行一个正整数n (n<= ...
- BZOJ2795&2890&3647[Poi2012]A Horrible Poem——hash
题目描述 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. 输入 第一行一个正整数n (n<= ...
- [BZOJ2795][Poi2012]A Horrible Poem
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 261 Solved: 150[Subm ...
随机推荐
- srm 534
250 Description 给你一个1*n的棋盘.两人轮流行动,每一个人能够把"o"向右移动到空格子.或者跨越连续两个"o"到空格子. 一个"o& ...
- 集团财务分析BI项目中的财务系统环境
我国集团化经营模式起步较晚,集团管控模式及管控力度各异,集团范围内财务信息化水平及统一程度不尽相同,因此在实施集团财务分析一类的BI商业智能项目的过程中,在不同的集团之间遇到的财务系统及核算数据环境也 ...
- hdu3416 Marriage Match IV【最短路+最大流】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297581.html ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...
- the convertion between string and BlobColumn
It's hard to find some samples about the convertion between string and BlobColumn.AddBlobData. It's ...
- 基于hash的文档判重——simhash
本文环境: python3.5 ubuntu 16.04 第三方库: jieba 文件寄于github: https://github.com/w392807287/angelo_tools.git ...
- 五毛的cocos2d-x学习笔记02-基本项目源码分析
class AppDelegate : private cocos2d::Application private表示私有继承,cocs2d是一个命名空间.私有继承下,Application类中的pri ...
- 杭电oj find your present (2)
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- CGI PHP安装
./configure --with-php-config=/usr/local/php/bin/php-config--with-pdo-mysql=/usr/local/mysql PDO——MY ...
- [LeetCode]题解(python):062-Unique Paths
题目来源: https://leetcode.com/problems/unique-paths/ 题意分析: 给定两个整型m,n.判断从一个m×n的矩阵里面,从(0,0)走到(m-1,n-1)一共有 ...
- vs2010中iostream.h出错
使用 #include <iostream> using namespace std; 替代 VS2010删除了所有非标准库,保留了C++标准库,iostream.h是以前旧版的库,VS2 ...