#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
int t,n;
ll baza=31,mod=1e9+7;
ll h[1000005],pw[1000005];
ll gethash(ll st,ll dr)
{
ll val=(h[dr]-(h[st-1]*pw[dr-st+1])%mod+mod)%mod;
return val;
}
int getans(int l,int r)
{
if(l>r)
return 0;
for(int last=l;last<=r;last++)
{
int lg=last-l+1;
int poz=r-lg+1;
if(poz>last)
{
ll h1=gethash(l,last);
ll h2=gethash(poz,r);
if(h1==h2)
return 2+getans(last+1,poz-1);
}
}
return 1;
}
void solve()
{
cin>>s;
n=s.size();
s=" "+s;
pw[0]=1;
for(int i=1;i<=n;i++)
{
h[i]=((h[i-1]*baza)%mod+s[i]-'a'+1)%mod;
pw[i]=(pw[i-1]*baza)%mod;
}
cout<<getans(1,n)<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
290 ms |
24456 KB |
Output is correct |
15 |
Correct |
170 ms |
22868 KB |
Output is correct |
16 |
Correct |
302 ms |
28856 KB |
Output is correct |
17 |
Correct |
151 ms |
14680 KB |
Output is correct |