제출 #464656

#제출 시각아이디문제언어결과실행 시간메모리
464656BERNARB01Palindromic Partitions (CEOI17_palindromic)C++17
100 / 100
615 ms43308 KiB
#include <bits/stdc++.h> using namespace std; struct p_hash { const int mod = (int) 1e9 + 21; const int mod2 = (int) 1e9 + 33; const int b = 53; const int b2 = 47; int n; vector<long long> h, h2, pb, pb2; p_hash() {} p_hash(const string& s) { n = s.length(); h.assign(n + 1, 0); h2.assign(n + 1, 0); pb.assign(n + 1, 1); pb2.assign(n + 1, 1); for (int i = 1; i <= n; i++) { pb[i] = pb[i - 1] * b % mod; pb2[i] = pb2[i - 1] * b2 % mod2; } for (int i = 0; i < n; i++) { h[i + 1] = (h[i] + (s[i] - 'a' + 1) * pb[i]) % mod; h2[i + 1] = (h2[i] + (s[i] - 'a' + 1) * pb2[i]) % mod2; } } pair<int, int> get(int l, int r) const { return { pb[n - l] * (h[r + 1] - h[l] + mod) % mod, pb2[n - l] * (h2[r + 1] - h2[l] + mod2) % mod2 }; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { string s; cin >> s; p_hash sh(s); int n = sh.n; int l = 0, r = n - 1, ans = 0; while (l <= r) { int ll = l, rr = r; while (1) { if (sh.get(l, ll) == sh.get(rr, r)) { break; } ll++; rr--; } if (rr <= ll) { ans++; break; } ans += 2; l = ll + 1; r = rr - 1; } cout << ans << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...