哈希WA#2求助
查看原帖
哈希WA#2求助
372780
Starw楼主2023/1/31 14:56

代码

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
ull base=131;
ull prime=1145141;
ull mod=212370440130137957ll;

ull a[9000005];
int tot;
char s[3005],t[3005];
int main(){
//	freopen("D.in","r",stdin);
//	freopen("D.out","w",stdout);
	int n,ans=0;
	scanf("%d",&n);
	scanf("%s",s+1);
	scanf("%s",t+1);
	for(int i=1;i<=n;i++){
		int k=i;
		for(int j=1;j<=n;j++)
			if(s[j]==t[k])k++;
		ans+=k-i;
		ull res=0;
		for(int j=i;j<=k;j++)
			res=(res*base+(ull)t[j])%mod+prime,
			a[++tot]=res;
	}
	sort(a+1,a+tot+1);
	for(int i=2;i<=tot;i++)
		if(a[i]==a[i-1])ans--;
	printf("%d",ans);
	return 0;
}
2023/1/31 14:56
加载中...