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 int long long
const int mod = 1e9 + 7;
const int p = 41;
int binpow(int a,int b){
if(!b)return 1;
int res = binpow(a,b/2);
res = (res * res) % mod;
if(b&1)res = (res * a) % mod;
return res;
}
void solve(){
string s;
cin >> s;
int n = s.size();
s = "." + s;
vector<int> pi(n+1,1),res(n+1,0);
for(int i=1;i<=n;++i){
pi[i] = pi[i-1] * p % mod;
res[i] = res[i-1] + ((pi[i] * (s[i] - 'a' + 1)) % mod);
res[i] %= mod;
}
int l=0,r=n;
int cnt = 1;
int ind = (n&1 ? n/2 + 1 : n/2);
for(int i=1;i<=n/2;++i){
int a = (res[i] - res[l] + mod) % mod;
a *= (l == 0 ? 1 : binpow(pi[l],mod-2));
a %= mod;
int b = (res[r] - res[r - (i - l)] + mod) % mod;
b *= binpow(pi[r - (i - l)],mod-2);
b %= mod;
// cout << a << ' ' << b << '\n';
if(a == b){
l = i;
r = n-l;
cnt += 2;
}
}
cout << cnt;
}
signed main(){
int t;
cin >> t;
for(int tt=0;tt<t;++tt){
solve();
cout << '\n';
}
}
Compilation message (stderr)
palindromic.cpp: In function 'void solve()':
palindromic.cpp:28:9: warning: unused variable 'ind' [-Wunused-variable]
28 | int ind = (n&1 ? n/2 + 1 : n/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... |