QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#838381 | #8073. String | FLtheLeatherman | TL | 0ms | 3964kb | C++14 | 1.8kb | 2024-12-31 08:37:19 | 2024-12-31 08:37:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int mod=998244353;
const int base=19260817;
const int MAXN=100010;
int n;
int poww[MAXN];
char s1[MAXN],s2[MAXN];
int b1[MAXN],b2[MAXN];
void init(){
poww[0]=1;
for(int i=1;i<=n;++i){
poww[i]=1ll*poww[i-1]*base%mod;
}
for(int i=1;i<=n;++i){
b1[i]=((1ll*b1[i-1]*base)%mod+s1[i])%mod;
}
for(int i=n;i>=1;--i){
b2[i]=((1ll*b2[i+1]*base)%mod+s2[i])%mod;
}
}
int m;
char s[MAXN];
int a1[MAXN],a2[MAXN];
void gao(){
a1[0]=0;
for(int i=1;i<=m;++i){
a1[i]=((1ll*a1[i-1]*base)%mod+s[i])%mod;
}
a2[m+1]=0;
for(int i=m;i>=1;--i){
a2[i]=((1ll*a2[i+1]*base)%mod+s[i])%mod;
}
}
bool check1(int l,int r){
int len=r-l+1;
return a1[len]==(b1[r]-(1ll*b1[l-1]*poww[r-l+1])%mod+mod)%mod;
}
bool check2(int l,int r){
int len=r-l+1;
// cout<<l<<' '<<r<<endl;
// cout<<a2[m-len+1]<<endl;
// cout<<(b2[l]-(1ll*b2[r+1]*poww[r-l+1])%mod+mod)%mod<<endl;
return a2[m-len+1]==(b2[l]-(1ll*b2[r+1]*poww[r-l+1])%mod+mod)%mod;
}
int query(int i){
int l=i,r=i+m-2;
int pos1=i-1;
while(l<=r){
int mid=(l+r)/2;
if(check1(i,mid)){
pos1=mid;
l=mid+1;
}
else {
r=mid-1;
}
}
// cout<<pos1<<endl;
l=i+1,r=i+m-1;
int pos2=i+m;
while(l<=r){
int mid=(l+r)/2;
if(check2(mid,i+m-1)){
pos2=mid;
r=mid-1;
}
else {
l=mid+1;
}
}
// cout<<pos2<<endl;
if(pos1==i-1)return 0;
if(pos2==i+m)return 0;
// cout<<pos1<<' '<<pos2<<endl;
return max(0,min(pos1-pos2+2,m-1));
}
int main(){
scanf("%s",s1+1);
scanf("%s",s2+1);
n=strlen(s1+1);
init();
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s+1);
m=strlen(s+1);
gao();
ll ans=0;
for(int i=1;i+m-1<=n;++i){
// cout<<i<<":\n";
ans+=query(i);
// exit(0);
}
printf("%lld\n",ans);
// exit(0);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3964kb
input:
aaababaa aababbca 7 aa abb aab ab abc bb ba
output:
3 1 3 2 2 1 0
result:
ok 7 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3920kb
input:
baabaabbaaaaabaabbababababaabbaababaaababbbabbabaa aabaabaabbbbbabaababbbbaaabbbaabaabaabbbabbabbbbab 10 bb abaaa aabb aab bb ba bba bab baa abba
output:
11 4 10 11 11 11 8 19 8 10
result:
ok 10 lines
Test #3:
score: -100
Time Limit Exceeded
input:
aababbababaaaaaabbababaaabbabbbbbbaaabbbbaaabaabaaabaaaabbbabababaaabbababbbaaaabbbbbaabaaababbaaaaaaabaabaabaabbbbbbaabbbabababbabbbbabbabababaaaabaaaaabbbbaababbbbaaabbbbbaabaabbaababbaaabaabaaabbbbaabababaaabbbbabaababababbbaababbabbbbbbabbbbaaabbaaabbbabbabaabbaaabaaaabbbbbbbaabaabbbbbbabbbbabbb...