#include <bits/stdc++.h>
/*
Use hashing
AAAAAAA AAAAAAA
BBBB BBBB
ABCABCA ABCABCA
-> A BC A BC A (rotations)
ABCDEABCDEABC -> ABC DE ABC DE ABC, original is not solution
Greedy works
*/
int n,t;
int hashbase;
int hashmod=1000000007;
long long binexp(int a,int b,int mod=hashmod){
b=(b%(mod-1)+(mod-1))%(mod-1);
long long e=a;
long long s=1;
while(b){
if(b&1)s*=e;
s%=mod;
e=e*e%mod;
b>>=1;
}
return s;
}
int main(){
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::mt19937 rgen(std::chrono::steady_clock().now().time_since_epoch().count());
std::cin>>t;
for(;t--;){
hashbase=std::uniform_int_distribution(100000000,1000000006)(rgen);
std::string s;
std::cin>>s;
n=s.size();
std::vector<long long> hashes(1);
long long chash=0;
for(int i=0;i<n;i++){
chash+=s[i]*binexp(hashbase,i,hashmod);
chash%=hashmod;
hashes.push_back(chash);
}
int progress=0;
int last=0;
int total=1;
for(int i=1;i*2<=n;i++){
long long hash1=(hashes[i]-hashes[progress]+hashmod)%hashmod*binexp(hashbase,(n-i)-progress,hashmod)%hashmod;
long long hash2=(hashes[n-progress]-hashes[n-i]+hashmod)%hashmod;
//std::cout<<hash1<<' '<<hash2<<'\n';
if(hash1==hash2){
//std::cout<<i-progress<<'\n';
progress=i;
total+=2;
if(i*2==n)total--;//no substr in between
}
}
std::cout<<total<<'\n';
}
return 0;
}
Compilation message
palindromic.cpp: In function 'int main()':
palindromic.cpp:53:13: warning: unused variable 'last' [-Wunused-variable]
53 | int last=0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
31 ms |
628 KB |
Output is correct |
11 |
Correct |
17 ms |
584 KB |
Output is correct |
12 |
Correct |
30 ms |
628 KB |
Output is correct |
13 |
Correct |
25 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
31 ms |
628 KB |
Output is correct |
11 |
Correct |
17 ms |
584 KB |
Output is correct |
12 |
Correct |
30 ms |
628 KB |
Output is correct |
13 |
Correct |
25 ms |
600 KB |
Output is correct |
14 |
Correct |
4590 ms |
27176 KB |
Output is correct |
15 |
Correct |
2440 ms |
21656 KB |
Output is correct |
16 |
Correct |
4375 ms |
26376 KB |
Output is correct |
17 |
Correct |
2356 ms |
18708 KB |
Output is correct |