SYZOJ 20220219 A 位置
作者:互联网
written on 2022-02-21
因为第一次写字符Hash,所以贴一个类模板,当做入门
#include<bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;
const int base=131;
int n;
char a[100005];
ull f[100005],p[100005];
ull Hash(int l,int r){return f[r]-f[l-1]*p[r-l+1];}
map<ull,bool> mp;
bool check(int x)
{
mp.clear();
for(int i=1;i<=n;i++)
{
if(i+x-1>n) break;
ull val=Hash(i,i+x-1);
if(mp[val]) return 0;
mp[val]=1;
}
return 1;
}
int main()
{
scanf("%d",&n);
scanf("%s",a+1);
p[0]=1;
for(int i=1;i<=n;i++)
{
f[i]=f[i-1]*base+(a[i]-'A'+1);
p[i]=p[i-1]*base;
}
int l=1,r=n,ans;
while(l<=r)
{
int mid=l+r>>1;
if(check(mid))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%d",ans);
}
标签:20220219,Hash,val,int,位置,SYZOJ,mid,mp,ull 来源: https://www.cnblogs.com/Freshair-qprt/p/16537594.html