#include <bits/stdc++.h>
// author: aykhn
using namespace std;
typedef long long ll;
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
const int mod = 1e9 + 7;
const int MXN = 1e6 + 1;
const int base = 12031;
int ph[MXN], p[MXN], invp[MXN];
int mult(int a, int b)
{
return (a * b) % mod;
}
int add(int a, int b)
{
return (a + b) % mod;
}
int pw(int a, int b, int c)
{
a %= c;
int res = 1;
while (b)
{
if (b & 1) res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
int get_hash(int l, int r)
{
return mult(add(ph[r], mod - ph[l - 1]), invp[l]);
}
void _()
{
string s;
cin >> s;
int n = s.length();
s = "#" + s;
ph[0] = 0;
for (int i = 1; i <= n; i++) ph[i] = add(ph[i - 1], mult(p[i], s[i] - 'a'));
int res = 0;
int f = 1;
int l = 1;
int r = n;
for (int i = 1; i <= n/2; i++)
{
if (get_hash(l, i) == get_hash(n - i + 1, r))
{
l = i + 1;
r = n - i;
res++;
}
}
cout << res * 2 + (l <= r) << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
p[0] = 1;
for (int i = 1; i < MXN; i++) p[i] = mult(p[i - 1], base);
for (int i = 0; i < MXN; i++) invp[i] = pw(p[i], mod - 2, mod);
int t = 1;
cin >> t;
for (int tt = 1; tt <= t; tt++)
{
_();
}
}
Compilation message
palindromic.cpp: In function 'void _()':
palindromic.cpp:54:9: warning: unused variable 'f' [-Wunused-variable]
54 | int f = 1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
16976 KB |
Output is correct |
2 |
Correct |
120 ms |
17036 KB |
Output is correct |
3 |
Correct |
120 ms |
17032 KB |
Output is correct |
4 |
Correct |
119 ms |
16980 KB |
Output is correct |
5 |
Correct |
120 ms |
17036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
16976 KB |
Output is correct |
2 |
Correct |
120 ms |
17036 KB |
Output is correct |
3 |
Correct |
120 ms |
17032 KB |
Output is correct |
4 |
Correct |
119 ms |
16980 KB |
Output is correct |
5 |
Correct |
120 ms |
17036 KB |
Output is correct |
6 |
Correct |
120 ms |
17040 KB |
Output is correct |
7 |
Correct |
119 ms |
17024 KB |
Output is correct |
8 |
Correct |
120 ms |
16980 KB |
Output is correct |
9 |
Correct |
120 ms |
17020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
16976 KB |
Output is correct |
2 |
Correct |
120 ms |
17036 KB |
Output is correct |
3 |
Correct |
120 ms |
17032 KB |
Output is correct |
4 |
Correct |
119 ms |
16980 KB |
Output is correct |
5 |
Correct |
120 ms |
17036 KB |
Output is correct |
6 |
Correct |
120 ms |
17040 KB |
Output is correct |
7 |
Correct |
119 ms |
17024 KB |
Output is correct |
8 |
Correct |
120 ms |
16980 KB |
Output is correct |
9 |
Correct |
120 ms |
17020 KB |
Output is correct |
10 |
Correct |
121 ms |
17232 KB |
Output is correct |
11 |
Correct |
120 ms |
16980 KB |
Output is correct |
12 |
Correct |
121 ms |
16976 KB |
Output is correct |
13 |
Correct |
121 ms |
17020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
16976 KB |
Output is correct |
2 |
Correct |
120 ms |
17036 KB |
Output is correct |
3 |
Correct |
120 ms |
17032 KB |
Output is correct |
4 |
Correct |
119 ms |
16980 KB |
Output is correct |
5 |
Correct |
120 ms |
17036 KB |
Output is correct |
6 |
Correct |
120 ms |
17040 KB |
Output is correct |
7 |
Correct |
119 ms |
17024 KB |
Output is correct |
8 |
Correct |
120 ms |
16980 KB |
Output is correct |
9 |
Correct |
120 ms |
17020 KB |
Output is correct |
10 |
Correct |
121 ms |
17232 KB |
Output is correct |
11 |
Correct |
120 ms |
16980 KB |
Output is correct |
12 |
Correct |
121 ms |
16976 KB |
Output is correct |
13 |
Correct |
121 ms |
17020 KB |
Output is correct |
14 |
Correct |
224 ms |
36252 KB |
Output is correct |
15 |
Correct |
177 ms |
31232 KB |
Output is correct |
16 |
Correct |
218 ms |
35472 KB |
Output is correct |
17 |
Correct |
177 ms |
27676 KB |
Output is correct |