# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
468650 | idk321 | Palindromic Partitions (CEOI17_palindromic) | C++17 | 119 ms | 44404 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;
string s;
const int N = 1000001;
const int MINF = -1000000000;
const int MOD = 1000000007;
const int P = 29;
int res[N];
int vis[N];
ll power[N];
int calc(int n) {
if (n == s.size() / 2) {
if (s.size() % 2 == 1) return 1;
return 0;
}
if (res[n]) return res[n];
ll hash1 = 0;
ll hash2 = 0;
int j = 0;
int cres = 1;
for (int i = n; i < s.size() / 2; i++, j++) {
hash2 *= P;
hash2 %= MOD;
hash1 += (s[i] - 'a') * power[j];
hash1 %= MOD;
hash2 += s[s.size() - 1 - i] - 'a';
hash2 %= MOD;
//cout << i << " " << hash1 << " " << hash2 << " " << s.size()<< endl;
if (hash1 == hash2) {
cres = max(cres, 2 + calc(i + 1));
break;
}
}
res[n] = cres;
return res[n];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
power[0] = 1;
for (int i = 1; i <N; i++) {
power[i] = power[i - 1] * P;
power[i] %= MOD;
}
int t;
cin >> t;
for (int t1 = 0; t1 < t; t1++) {
cin >> s;
int cres = 0;
for (int i = 0; i < s.size() / 2 + 1; i++) res[i] = 0;
cout << calc(0) << "\n";
}
}
/*
4
bonobo
deleted
racecar
racecars
*/
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... |