#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define S second
#define F first
#define pb push_back
#define SZ(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define err(x) cout<<#x<<": "<<x<<'\n';
#define int long long
using namespace std;
const int MOD=98765431;
const int M=31;
int pw(int a,int b){
if(!b) return 1;
int rt=pw(a,b/2);
rt=(rt*rt)%MOD;
if(b&1) rt=(rt*a)%MOD;
return rt;
}
int inv(int x){
return pw(x,MOD-2);
}
void Main(){
int n;
string s; cin>>s; n=SZ(s); s='$'+s;
int ps[n+1]={};
int PW[n+1];
PW[0]=1;
FOR(i,1,n) PW[i]=(PW[i-1]*M)%MOD;
FOR(i,1,n){
ps[i]=ps[i-1];
ps[i]+=((s[i]-'a'+1)*PW[i-1])%MOD;
}
int l=1,r=n;
int ans=0;
while(r>l){
int mid1=(r+l)/2;
int mid2=mid1+1;
if((r-l+1)&1) mid1--;
int t=-1;
FOR(i,0,mid1-l){
int hsh1=ps[i+l]-ps[l-1];
hsh1=hsh1*inv(PW[l-1]); hsh1=((hsh1%MOD)+MOD)%MOD;
int hsh2=ps[r]-ps[r-i-1];
hsh2=hsh2*inv(PW[r-i-1]); hsh2=((hsh2%MOD)+MOD)%MOD;
if(hsh1==hsh2) {t=i; break;}
}
if(t==-1) {
ans++;
break;
}else{
l=l+t+1;
r=r-t-1;
ans+=2;
}
}
if(r==l) ans++;
cout<<ans<<'\n';
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int T; cin>>T;
while(T--) Main();
return 0;
}
Compilation message
palindromic.cpp: In function 'void Main()':
palindromic.cpp:43:8: warning: unused variable 'mid2' [-Wunused-variable]
43 | int mid2=mid1+1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Incorrect |
21 ms |
600 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Incorrect |
21 ms |
600 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |