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 <iostream>
using namespace std;
#define int long long
#define ll long long
const ll base=717;
const ll mod=1e9+9;
const int N=1e6+10;
ll n,pre[N],pw[N];
void Hash(string& s)
{
n=(int)s.size();
pre[0]=0;
for(int i=1;i<=n;i++)
{
pre[i]=(pre[i-1]*base)%mod;
pre[i]+=(s[i-1]-'a');
pre[i]%=mod;
}
pw[0]=1;
for(int i=1;i<=n;i++)
pw[i]=(pw[i-1]*base)%mod;
}
// (l,r)
// s[r]
// s[l]*(r-l) + .. s[r]
// s[l-1]
// s[l-1]
int get(int l,int r)//inclusive 1-based indexing
{
return (pre[r]+mod-((pre[l-1]*pw[r-l+1])%mod))%mod;
}
void solve()
{
string s;
cin>>s;
Hash(s);
int l=0;
int r=n+1;
int cnt=0;
while(l<r)
{
for(int len=1;len<=(r-l-1);len++)
{
if(get(l+1,l+len)==get(r-len,r-1))
{
// cout<<"First match "<<l+1<<' '<<l+len<<' '<<r-len<<' '<<r-1<<endl;
cnt++;
cnt+=((l+1!=(r-len)) or ((l+len)!=(r-1)));
l+=len;
r-=len;
break;
}
}
}
cout<<cnt<<endl;
// both endpoints are exclusive (l,r)
}
signed main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}
# | 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... |