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 F first
#define S second
#define pii pair<int,int>
#define piii pair<pair<int,int>,int>
const int N = 1e6+10;
const int M = 101;
const LL INF = 1e9;
const LL LINF = 2e18;
const LL MOD = 1e9+7;
const double PI = 3.141592653589793;
string s;
int chash[N];
int powers[N];
int add(LL a,LL b){
return (a+b)%MOD;
}
int mult(LL a,LL b){
return (a*b)%MOD;
}
bool check(int l1,int r1,int l2,int r2){
LL h1 = ((chash[r2]-chash[l2-1])%MOD+MOD)%MOD;
LL h2 = ((chash[r1]-(l1?chash[l1-1]:0))%MOD+MOD)%MOD;
h2 = mult(h2,powers[l2-l1]);
return h1==h2;
}
int solve(int l,int r){
if(l>r)
return 0;
for(int i=l;i<(l+r+1)/2;i++)
if(check(l,i,r-(i-l),r))
return 2+solve(i+1,r-(i-l)-1);
return 1;
}
int main(){
//freopen("out.txt","w",stdout);
powers[0] = 1;
for(int i=1;i<=1e6;i++)
powers[i] = mult(powers[i-1],31);
int t;
scanf("%d",&t);
while(t--){
cin>>s;
int n = s.size();
for(int i=0;i<n;i++)
chash[i] = add(i?chash[i-1]:0,mult(s[i]-'a'+1,powers[i]));
printf("%d\n",solve(0,n-1));
}
}
Compilation message (stderr)
palindromic.cpp: In function 'int main()':
palindromic.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d",&t);
| ~~~~~^~~~~~~~~
# | 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... |