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;
#define int long long
const int MOD = 1e9 + 7;
const int base = 9973;
int add (int a, int b) {
return (MOD + (a + b) % MOD) % MOD;
}
int sub (int a, int b) {
return ((a - b) % MOD + MOD) % MOD;
}
int mul (int a, int b) {
return (a * b) % MOD;
}
int n;
string s;
int pw[1000001];
int p_hash[1000001];
int dp[1000001];
inline int get (int l, int r) {
return sub(p_hash[r + 1], mul(p_hash[l], pw[r - l + 1]));
}
int ans (int l, int r) {
if (l == r) {
return 1;
}
if (l > r) {
return 0;
}
int &ret = dp[l];
if (ret != -1) return ret;
ret = 1;
for (int i = l; i < n/2; i++) {
int x = i - l + 1;
if (get(l, i) == get(r - x + 1, r)) {
ret = ans(i + 1, r - x) + 2 ;
break;
}
}
return ret;
}
void solve () {
cin >> s; n = s.length();
for (int i = 1; i <= n; i++) {
dp[i - 1] = -1;
p_hash[i] = add(mul(p_hash[i - 1], base), s[i - 1]);
}
cout << ans(0, n - 1) << '\n';
}
signed main () {
pw[0] = 1;
for (int i = 1; i <= 1e6; i++) {
pw[i] = mul(base, pw[i - 1]);
}
int t;
cin >> t;
while (t--) solve();
}
# | 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... |