#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
bool remender(ll a , ll b){return a%b;}
//freopen("problemname.in", "r", stdin);
//freopen("problemname.out", "w", stdout);
ll store1[1000002];
vector<ll> store;
const int P = 31;
ll add(ll x, ll y){
return (x+y)%mod;
}
ll multi(ll x , ll y){
return (x*y)%mod;
}
ll power(ll x , ll y){
if(x == P && y <= 1000000 && store1[y] != -1)return store1[y];
x %= mod;
ll res = 1;
while(y > 0){
if(y&1)res=multi(res,x);
y=y>>1;
x = multi(x,x);
}
if(x == P && y <= 1000000)store1[y] = res;
return res;
}
ll inv(ll n){
if(store.size() < n + 1)return power(n,mod-2);
return store[n];
}
void solve(){
int n;
string s;
cin >> s;
n = s.size();
string a , b;
a = "";
b = "";
for(int i = 0; i < n/2; i++)a += s[i];
for(int i = (n + 1)/2; i < n; i++)b += s[i];
int N = a.size();
ll hash[N];
ll hash1[N];
for(int i = 0; i < N; i++){
hash[i] = multi(power(P,i),a[i]-'a');
if(i)hash[i] = add(hash[i],hash[i-1]);
hash1[i] = multi(power(P,i),b[i]-'a');
if(i)hash1[i] = add(hash1[i],hash1[i-1]);
}
int ans = 0;
int i = 0;
for(int j = 0; j < N; j++){
ll x = hash[j] - (i > 0 ? hash[i-1] : 0);
x += mod;
x %= mod;
x = multi(x , inv(i));
ll y = hash1[N-(i+1)];
if(j < N - 1){
y -= hash1[N-(j+2)];
y += mod;
y %= mod;
}
y = multi(y,inv(N-(j + 1)));
//cout << i << ' ' << j << ' ' << x << ' ' << y << '\n';
if(x == y){
ans += 2;
i = j + 1;
}
}
if(n % 2 || i != N)ans++;
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(store1,-1,sizeof store1);
for(int i = 0; i <= 1000000; i++){
power(P,i);
store.pb(inv(power(P,i)));
}
int t;cin >> t;while(t--)
solve();
return 0;
}
Compilation message
palindromic.cpp: In function 'long long int inv(long long int)':
palindromic.cpp:51:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
51 | if(store.size() < n + 1)return power(n,mod-2);
| ~~~~~~~~~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
16476 KB |
Output is correct |
2 |
Correct |
320 ms |
16440 KB |
Output is correct |
3 |
Correct |
319 ms |
16564 KB |
Output is correct |
4 |
Correct |
314 ms |
16452 KB |
Output is correct |
5 |
Correct |
324 ms |
16468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
16476 KB |
Output is correct |
2 |
Correct |
320 ms |
16440 KB |
Output is correct |
3 |
Correct |
319 ms |
16564 KB |
Output is correct |
4 |
Correct |
314 ms |
16452 KB |
Output is correct |
5 |
Correct |
324 ms |
16468 KB |
Output is correct |
6 |
Correct |
323 ms |
16476 KB |
Output is correct |
7 |
Correct |
326 ms |
16456 KB |
Output is correct |
8 |
Correct |
321 ms |
16476 KB |
Output is correct |
9 |
Correct |
322 ms |
16568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
16476 KB |
Output is correct |
2 |
Correct |
320 ms |
16440 KB |
Output is correct |
3 |
Correct |
319 ms |
16564 KB |
Output is correct |
4 |
Correct |
314 ms |
16452 KB |
Output is correct |
5 |
Correct |
324 ms |
16468 KB |
Output is correct |
6 |
Correct |
323 ms |
16476 KB |
Output is correct |
7 |
Correct |
326 ms |
16456 KB |
Output is correct |
8 |
Correct |
321 ms |
16476 KB |
Output is correct |
9 |
Correct |
322 ms |
16568 KB |
Output is correct |
10 |
Correct |
326 ms |
16388 KB |
Output is correct |
11 |
Correct |
330 ms |
16452 KB |
Output is correct |
12 |
Correct |
334 ms |
16372 KB |
Output is correct |
13 |
Correct |
341 ms |
16520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
16476 KB |
Output is correct |
2 |
Correct |
320 ms |
16440 KB |
Output is correct |
3 |
Correct |
319 ms |
16564 KB |
Output is correct |
4 |
Correct |
314 ms |
16452 KB |
Output is correct |
5 |
Correct |
324 ms |
16468 KB |
Output is correct |
6 |
Correct |
323 ms |
16476 KB |
Output is correct |
7 |
Correct |
326 ms |
16456 KB |
Output is correct |
8 |
Correct |
321 ms |
16476 KB |
Output is correct |
9 |
Correct |
322 ms |
16568 KB |
Output is correct |
10 |
Correct |
326 ms |
16388 KB |
Output is correct |
11 |
Correct |
330 ms |
16452 KB |
Output is correct |
12 |
Correct |
334 ms |
16372 KB |
Output is correct |
13 |
Correct |
341 ms |
16520 KB |
Output is correct |
14 |
Incorrect |
1273 ms |
29072 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |