#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
const ll Mod = 1000000007;
const ll Mod2 = 999999999989;
const int maxN = 1e6 + 2;
int n;
int base = 29;
ll pre[2][maxN], suf[2][maxN], power[2][maxN];
string s;
ll gethash(int l, int r, int t){
ll tem = pre[t][r] - pre[t][l - 1] * power[t][r - l + 1];
if (t) return (tem % Mod2 + Mod2) % Mod2;
return (tem % Mod + Mod) % Mod;
}
bool cmp(int l1, int r1, int l2, int r2){
return gethash(l1, r1, 1) == gethash(l2, r2, 1)
&& gethash(l1, r1, 0) == gethash(l2, r2, 0);
}
void Init(){
cin >> s;
n = s.length();
for (int i = 1; i <= n; ++i){
pre[0][i] = pre[0][i - 1] * base + (s[i - 1] - 'a');
pre[1][i] = pre[1][i - 1] * base + (s[i - 1] - 'a');
pre[0][i] %= Mod;
pre[1][i] %= Mod2;
}
ll ans = 0;
int i = 1, j = n;
while (i <= j){
int l = i, r = j;
while (!cmp(i, l, r, j)){
++l, --r;
}
if (l < r) ans += 2;
else ++ans;
i = l + 1, j = r - 1;
}
cout << ans << "\n";
}
int main(){
if (fopen(taskname".txt", "r")){
freopen(taskname".txt", "r", stdin);
}
faster
power[0][0] = power[1][0] = 1;
for (int i = 1; i <= 1e6 + 1; ++i){
power[0][i] = power[0][i - 1] * base % Mod;
power[1][i] = power[1][i - 1] * base % Mod2;
}
//freopen(taskname.inp, "r", stdin)
//freopen(taskname.out, "w", stdout)
int tt; cin >> tt; while (tt--)
Init();
}
Compilation message
palindromic.cpp: In function 'int main()':
palindromic.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen(taskname".txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |