#include<bits/stdc++.h>
#define pii pair<long,long>
#define ll long long
#define f first
#define s second
using namespace std;
const int mxn=1000008;
const ll p1=20000003,p2=500009,p3=100200011,p4=1e9+9;
char s[mxn];
pii getHash(pii x,ll c)
{
x.f=x.f*p1 % p3;
x.s=x.s*p2 % p4;
x.f+=c*p1;
x.f%=p3;
x.s+=c*p2;
x.s%=p4;
return x;
}
int solve(int b,int e)
{
if(b>e)return 0;
if(b==e)return 1;
if(e-b==1)
{
if(s[b]==s[e])return 2;
return 1;
}
pii h1=make_pair(0,0);
pii h2=make_pair(0,0);
ll P1=p1,P2=p2;
while(b<e)
{
ll c=s[b]-'a';
h1={(h1.f+ c*P1) % p3, (h1.s+ c*P2) % p4};
h2=getHash(h2,s[e]-'a');
if(h1==h2)return 2+solve(b+1,e-1);
b++;e--;
P1=p1*P1 % p3;
P2=P2*p2 % p4;
}
return 1;
}
void solve()
{
scanf("%s",s);
int n=strlen(s);
int res=solve(0,n-1);
printf("%d\n",res);
}
int main()
{
//freopen("input.txt","r",stdin);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
Compilation message
palindromic.cpp: In function 'void solve()':
palindromic.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s);
~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
163 ms |
42360 KB |
Output is correct |
15 |
Correct |
81 ms |
26364 KB |
Output is correct |
16 |
Correct |
106 ms |
10616 KB |
Output is correct |
17 |
Correct |
78 ms |
14200 KB |
Output is correct |