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>
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 (stderr)
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 |
---|
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... |