1 solutions
-
0
用字符串哈希做的,结果发现标签是 KMP……?
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; const int N = 1e6 + 10, B = 131; multiset<ull> st; ull p[N], h1[N], h2[N]; ull myHash(string s, ull h[]) { for (int i = 1; i <= s.length(); ++i) { h[i] = h[i - 1] * B + s[i - 1]; } return h[s.length()]; } ull subHash(ull h[], int l, int r) { return h[r] - h[l - 1] * p[r - l + 1]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); p[0] = 1; for (int i = 1; i < N; i++) { p[i] = p[i - 1] * B; } int l; string s; cin >> l >> s; myHash(s, h1); for (int i = 1; i <= l; i++) { ull t = subHash(h1, 1, i); bool flag = true; for (int j = 1; j <= l / i; j++) { int x = i * (j - 1) + 1, y = i * j; if (subHash(h1, x, y) != t) { flag = false; break; } } if (!flag) continue; if (l % i != 0) { ull t1 = subHash(h1, l - l % i + 1, l); ull t2 = subHash(h1, 1, l % i); if (t1 == t2) { cout << i << endl; return 0; } } else { cout << i << endl; return 0; } } return 0; }
- 1
Information
- ID
- 3336
- Time
- 1000ms
- Memory
- 125MiB
- Difficulty
- 4
- Tags
- # Submissions
- 1
- Accepted
- 1
- Uploaded By