求改正
查看原帖
求改正
1025321
midsummer_zyl楼主2023/10/28 16:55

求助

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 5e5 + 10;
const int B = 233;
LL h[N], base[N], s2[N];
LL S[N][26], n;
char s[N];
void init() {
	base[0] = 1;
	for (LL i = 1; i <= n; i++) {
		h[i] = h[i - 1] * B + s[i];
		base[i] = base[i - 1] * B;
		for (LL j = 0; j < 26; j++)
			S[i][j] = S[i - 1][j] + ((s[i] - 'a') == j);
	}
}
LL Hash(LL l, LL r) {
	return h[l + r - 1] - h[l - 1] * base[r];
}
bool check(LL l, LL r, LL x) {
	if(Hash(l, r - l + 1 - x) == Hash(l + x, r - l + 1 - x))
		return true;
	else return false;
}
LL gcd(LL a, LL b) {
	if(b == 0) return a;
	else return gcd(b, a % b);
}
int main() {
	cin >> n; scanf("%s", s + 1);
	init();
	LL q; cin >> q;
	while(n--) {
		LL l, r;
		cin >> l >> r;
		LL num = r - l + 1;
		LL i;
		for (i = 0; i < 26; i++)
			num = gcd(num, S[r][i] - S[l - 1][i]);
		if(num == 1) {
			cout << r - l + 1 << endl;
			continue;
		}
		bool flag = false;
		for (i = 1; i * i <= num; i++) {
			if(num % i == 0) {
				LL xx = (r - l + 1) / num * i;
				LL yy = r - l + 1 - xx;
				if(Hash(l, yy) == Hash(l + xx, yy)) {
					cout << xx << endl;
					flag = true;
					break;
				}
			}
		}
		if(flag) continue;
		for (i--; i; i--) {
			if(num % i == 0) {
				LL xx = (r - l + 1) / i;
				LL yy = r - l + 1 - xx;
				if(Hash(l, yy) == Hash(l + xx, yy)) {
					cout << xx << endl;
					break;
				}
			}
		}
	}
	return 0;
}

输出

1
3
5
5
5
5
5
5
2023/10/28 16:55
加载中...