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>
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define FORD(i, r, l) for(int i = (r); i >= (l); --i)
#define REP(i, l, r) for(int i = (l); i < (r); ++i)
#define REPD(i, r, l) for(int i = (r) - 1; i >= (l); --i)
using namespace std;
typedef unsigned long long ull;
const int N = 1e6 + 5;
const ull base = 27;
int t, n;
string s;
ull B[N], H[N];
void Enter() {
cin >> s;
n = s.length();
s = ' ' + s;
}
ull getHash(int l, int r) {
return H[r] - H[l - 1] * B[r - l + 1];
}
void Process() {
B[0] = 1;
FOR(i, 1, n) B[i] = B[i - 1] * base;
FOR(i, 1, n) H[i] = H[i - 1] * base + s[i] - 'a' + 1;
int res = (n == 1), len = 0;
FOR(i, 1, n/2) {
int j = i;
while (j <= n/2 && getHash(i, j) != getHash(n - j + 1, n - i + 1)) ++j;
i = j;
if (i > n/2) { ++res; break; }
res += 2;
if (i * 2 + 1 == n) ++res;
}
cout << res << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
if (fopen("palindromic.inp", "r")) freopen("palindromic.inp", "r", stdin);
cin >> t;
while (t--) {
Enter();
Process();
}
}
Compilation message (stderr)
palindromic.cpp: In function 'void Process()':
palindromic.cpp:29:25: warning: unused variable 'len' [-Wunused-variable]
29 | int res = (n == 1), len = 0;
| ^~~
palindromic.cpp: In function 'int main()':
palindromic.cpp:44:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | if (fopen("palindromic.inp", "r")) freopen("palindromic.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |