This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)1e6 + 5;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const int MOD2 = (int)1e9 + 9;
const int P = 31;
const int P2 = 37;
int pref[MAXN], pref2[MAXN];
int PW[MAXN], PW2[MAXN];
char s[MAXN];
int n, ans;
int addMod(int a, int b, int m) {
a += b;
if (m <= a) {
a -= m;
}
return a;
}
int mulMod(int a, int b, int m) {
return a * 1ll * b % m;
}
void pre() {
PW[0] = 1;
PW2[0] = 1;
for (int i = 1; i < MAXN; ++i) {
PW[i] = mulMod(PW[i - 1], P, MOD);
PW2[i] = mulMod(PW2[i - 1], P2, MOD2);
}
}
bool good(int l, int r) {
int l2 = n - r + 1;
int r2 = n - l + 1;
int a1 = addMod(pref[r], MOD - pref[l - 1], MOD);
int a2 = addMod(pref2[r], MOD2 - pref2[l - 1], MOD2);
int b1 = addMod(pref[r2], MOD - pref[l2 - 1], MOD);
int b2 = addMod(pref2[r2], MOD2 - pref2[l2 - 1], MOD2);
a1 = mulMod(a1, PW[n - l], MOD);
a2 = mulMod(a2, PW2[n - l], MOD2);
b1 = mulMod(b1, PW[n - l2], MOD);
b2 = mulMod(b2, PW2[n - l2], MOD2);
return a1 == b1 && a2 == b2;
}
void solve() {
scanf("%s", s + 1);
for (n = 1; s[n]; ++n);
--n;
ans = 0;
for (int i = 1; i <= n; ++i) {
pref[i] = addMod(pref[i - 1], mulMod(s[i] - 'a' + 1, PW[i], MOD), MOD);
pref2[i] = addMod(pref2[i - 1], mulMod(s[i] - 'a' + 1, PW2[i], MOD2), MOD2);
}
for (int l = 1, r; l <= n; l = r + 1) {
r = l;
while (r * 2 <= n && !good(l, r)) {
++r;
}
if (r * 2 > n) {
++ans;
break;
}
ans += 2;
if (r * 2 == n) {
break;
}
}
printf("%d\n", ans);
}
int main() {
int tt;
scanf("%d", &tt);
pre();
while (tt--) {
solve();
}
return 0;
}
Compilation message (stderr)
palindromic.cpp: In function 'void solve()':
palindromic.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s + 1);
~~~~~^~~~~~~~~~~~~
palindromic.cpp: In function 'int main()':
palindromic.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &tt);
~~~~~^~~~~~~~~~~
# | 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... |