#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Palindromic Partitions"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 1e6 + 7
using namespace std;
const ll mod = 972663749;
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n;
ll H[maxn], pw[maxn];
string a;
ll get(int l, int r) {
return (H[r] - H[l - 1] * pw[r - l + 1] + mod * mod) % mod;
}
void solve()
{
cin >> a;
n = a.size(), a = ' ' + a;
fi(i, 1, n) H[i] = (H[i - 1] * base + a[i] - 'a') % mod;
int l = 1, r = n;
// cout << get(3, 3) << " " << get(5, 5);
//
int ans = 0;
while(l <= r)
{
int R = l, L = r;
while(R < L) {
if(get(l, R) != get(L, r)) ++R, --L;
else
{
l = R + 1, r = L - 1;
ans += 2;
break;
}
}
if(R >= L) {
ans ++;
break;
}
}
cout << ans << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
cin >> nTest;
pw[0] = 1;
fi(i, 1, maxn - 2) pw[i] = (pw[i - 1] * base) % mod;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
palindromic.cpp: In function 'int main()':
palindromic.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8796 KB |
Output is correct |
2 |
Correct |
5 ms |
8796 KB |
Output is correct |
3 |
Correct |
5 ms |
8796 KB |
Output is correct |
4 |
Correct |
5 ms |
8796 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8796 KB |
Output is correct |
2 |
Correct |
5 ms |
8796 KB |
Output is correct |
3 |
Correct |
5 ms |
8796 KB |
Output is correct |
4 |
Correct |
5 ms |
8796 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
5 ms |
8792 KB |
Output is correct |
7 |
Correct |
5 ms |
8792 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
5 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8796 KB |
Output is correct |
2 |
Correct |
5 ms |
8796 KB |
Output is correct |
3 |
Correct |
5 ms |
8796 KB |
Output is correct |
4 |
Correct |
5 ms |
8796 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
5 ms |
8792 KB |
Output is correct |
7 |
Correct |
5 ms |
8792 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
5 ms |
8796 KB |
Output is correct |
10 |
Correct |
6 ms |
8796 KB |
Output is correct |
11 |
Correct |
6 ms |
8840 KB |
Output is correct |
12 |
Correct |
6 ms |
8796 KB |
Output is correct |
13 |
Correct |
6 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8796 KB |
Output is correct |
2 |
Correct |
5 ms |
8796 KB |
Output is correct |
3 |
Correct |
5 ms |
8796 KB |
Output is correct |
4 |
Correct |
5 ms |
8796 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
5 ms |
8792 KB |
Output is correct |
7 |
Correct |
5 ms |
8792 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
5 ms |
8796 KB |
Output is correct |
10 |
Correct |
6 ms |
8796 KB |
Output is correct |
11 |
Correct |
6 ms |
8840 KB |
Output is correct |
12 |
Correct |
6 ms |
8796 KB |
Output is correct |
13 |
Correct |
6 ms |
8796 KB |
Output is correct |
14 |
Correct |
100 ms |
17956 KB |
Output is correct |
15 |
Correct |
57 ms |
17948 KB |
Output is correct |
16 |
Correct |
95 ms |
19536 KB |
Output is correct |
17 |
Correct |
56 ms |
13956 KB |
Output is correct |