【BZOJ2795】[Poi2012]A Horrible Poem【Hash】【GCD】【暴力】
2016-07-14 12:12
405 查看
【题目链接】
【POPOQQQ的题解】
跑了倒数rk5...
/* Think Thank Thunk */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 500005;
int n, m, sum[26][maxn];
char str[maxn];
ULL hash[2][maxn], power[2][maxn], seed[] = {233, 997}, mod[] = {1000000009, 1000000007};
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline int gcd(int a, int b) {
for(; b; b ^= a ^= b ^= a %= b);
return a;
}
inline bool check(int l, int r, int len) {
len = r - l + 1 - len;
for(int j = 0; j < 2; j++) {
ULL a = (hash[j][l + len - 1] + mod[j] - (hash[j][l - 1] * power[j][len]) % mod[j]) % mod[j];
ULL b = (hash[j][r] + mod[j] - (hash[j][r - len] * power[j][len]) % mod[j]) % mod[j];
if(a != b) return 0;
}
return 1;
}
inline int query(int l, int r) {
int len = r - l + 1;
int k = len;
for(int i = 0; i < 26; i++) k = gcd(k, sum[i][r] - sum[i][l - 1]);
int res = len;
for(int i = 1; i * i <= k; i++) if(k % i == 0) {
if(check(l, r, len / i)) res = min(res, len / i);
if(check(l, r, len / (k / i))) res = min(res, len / (k / i));
}
return res;
}
int main() {
n = iread();
scanf("%s", str + 1);
for(int j = 0; j < 26; j++) for(int i = 1; i <= n; i++)
sum[j][i] = sum[j][i - 1] + (str[i] == (j + 'a'));
power[0][0] = power[1][0] = 1;
for(int j = 0; j < 2; j++) for(int i = 1; i <= n; i++) {
hash[j][i] = (hash[j][i - 1] * seed[j] + (str[i] - 'a')) % mod[j];
power[j][i] = (power[j][i - 1] * seed[j]) % mod[j];
}
m = iread();
while(m--) {
int l = iread(), r = iread();
printf("%d\n", query(l, r));
}
return 0;
}
【POPOQQQ的题解】
跑了倒数rk5...
/* Think Thank Thunk */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 500005;
int n, m, sum[26][maxn];
char str[maxn];
ULL hash[2][maxn], power[2][maxn], seed[] = {233, 997}, mod[] = {1000000009, 1000000007};
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline int gcd(int a, int b) {
for(; b; b ^= a ^= b ^= a %= b);
return a;
}
inline bool check(int l, int r, int len) {
len = r - l + 1 - len;
for(int j = 0; j < 2; j++) {
ULL a = (hash[j][l + len - 1] + mod[j] - (hash[j][l - 1] * power[j][len]) % mod[j]) % mod[j];
ULL b = (hash[j][r] + mod[j] - (hash[j][r - len] * power[j][len]) % mod[j]) % mod[j];
if(a != b) return 0;
}
return 1;
}
inline int query(int l, int r) {
int len = r - l + 1;
int k = len;
for(int i = 0; i < 26; i++) k = gcd(k, sum[i][r] - sum[i][l - 1]);
int res = len;
for(int i = 1; i * i <= k; i++) if(k % i == 0) {
if(check(l, r, len / i)) res = min(res, len / i);
if(check(l, r, len / (k / i))) res = min(res, len / (k / i));
}
return res;
}
int main() {
n = iread();
scanf("%s", str + 1);
for(int j = 0; j < 26; j++) for(int i = 1; i <= n; i++)
sum[j][i] = sum[j][i - 1] + (str[i] == (j + 'a'));
power[0][0] = power[1][0] = 1;
for(int j = 0; j < 2; j++) for(int i = 1; i <= n; i++) {
hash[j][i] = (hash[j][i - 1] * seed[j] + (str[i] - 'a')) % mod[j];
power[j][i] = (power[j][i - 1] * seed[j]) % mod[j];
}
m = iread();
while(m--) {
int l = iread(), r = iread();
printf("%d\n", query(l, r));
}
return 0;
}
相关文章推荐
- a标签href中事javascript
- JZOJ1728. Antimonotonicity
- Window Pains-POJ2585
- docker 命令详解
- 签名算法PHP和Java的不同实现
- 源码-PL/SQL从入门到精通-第三章-变量和类型-Part 1
- 使用 position: sticky 达到粘性元素区域悬浮效果
- http://blog.csdn.net/nomousewch/article/category/1424903
- java.lang.reflect.MalformedParameterizedTypeException
- shell学习-6-运算符
- strlen()
- MySQL 5.7 参数 – log_timestamps
- HDFS客户端的权限错误:Permission denied
- git经验
- Java 封装的详解
- linux 监控进程是否存在
- permit过滤由任意键值组成的嵌套 Hash
- QSQLITE
- Android自定义View制作动态炫酷按钮实例解析
- 相思成灾