QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#838380#8073. StringFLtheLeathermanWA 1ms5924kbC++141.8kb2024-12-31 08:32:562024-12-31 08:32:58

Judging History

你现在查看的是最新测评结果

  • [2024-12-31 08:32:58]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5924kb
  • [2024-12-31 08:32:56]
  • 提交

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=n;
	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=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: 3976kb

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: -100
Wrong Answer
time: 1ms
memory: 5924kb

input:

baabaabbaaaaabaabbababababaabbaababaaababbbabbabaa
aabaabaabbbbbabaababbbbaaabbbaabaabaabbbabbabbbbab
10
bb
abaaa
aabb
aab
bb
ba
bba
bab
baa
abba

output:

11
4
13
15
14
12
13
21
12
12

result:

wrong answer 3rd lines differ - expected: '10', found: '13'