# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
861958 | anhphant | Palindromic Partitions (CEOI17_palindromic) | C++14 | 118 ms | 28756 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const ll mod = 1e9 + 9;
const ll base = 9973;
string S;
int N;
ll basepow[1000007], pre[1000007];
ll getHash(int i, int j) {
ll r = pre[j];
ll l = (pre[i - 1] * basepow[j - i + 1]) % mod;
return (r - l + mod) % mod;
}
ll DP[1000007];
void solve() {
cin >> S;
int N = S.size();
S = " " + S;
for(int i = 1; i <= N; ++i) {
pre[i] = (pre[i - 1] * base + S[i] - 'a' + 1) % mod;
}
ll L = 1, R = N;
ll ans = 0;
while(L <= R) {
//cerr << L << " " << R << endl;
if (L == R) {
ans++;
break;
}
for(int i = L; i <= N / 2; ++i) {
//cerr << "i = " << i << endl;
if (getHash(L, i) == getHash(N - i + 1, R)) {
ans += 2;
L = i + 1;
R = N - i;
goto GT;
}
}
//cerr << "end : " << L << " " << R << endl;
ans += (L < R);
break;
GT:;
if (L == R) {
ans++;
break;
}
}
cout << ans << endl;
}
int main() {
ios_base :: sync_with_stdio(0);
cin.tie(0); cout.tie(0); cerr.tie(0);
if (fopen("FILE.INP", "r")) {
freopen("FILE.INP", "r", stdin);
freopen("FILE.OUT", "w", stdout);
}
basepow[0] = 1;
for(int i = 1; i <= 1000000; ++i) basepow[i] = (basepow[i - 1] * base) % mod;
int Tc;
cin >> Tc;
for(int _ = 1; _ <= Tc; ++_) {
//cerr << " _ = " << _ << endl;
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |