「字符串」哈希板子

单哈希

1.自然溢出哈希

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef unsigned long long ull;
const int maxn=10000+5,maxm=1e5+5;
int n,m,ans=1;
ull base=131,a[maxn];
char s[maxn];
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
	return s*w;
}
ull hashh(char s[]){
	int len=strlen(s);
	ull anss=0;
	for(int i=0;i<len;i++){
		anss=anss*base+(ull)s[i];
	}
	return anss;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		cin>>s;
		a[i]=hashh(s);
	}
	sort(a+1,a+n+1);
	for(int i=2;i<=n;i++){
		if(a[i]!=a[i-1])ans++;
	}
	cout<<ans;
}

2.单哈希(哈希冲突概率极高)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef unsigned long long ull;
const int maxn=10000+5,maxm=1e5+5;
int n,m,ans=1;
int base=131,a[maxn],mol=(1<<16)-1;
char s[maxn];
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
	return s*w;
}
int hashh(char s[]){
	int anss=0,len=strlen(s);
	for(int i=0;i<len;i++){
		anss=(anss*base+s[i])%mol;
	}
	return anss;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		cin>>s;
		a[i]=hashh(s);
	}
	sort(a+1,a+n+1);
	for(int i=2;i<=n;i++){
		if(a[i-1]!=a[i])ans++;
	}
	cout<<ans;
}

双哈希

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef unsigned long long ull;
const int maxn=10000+5,maxm=1e5+5;
int n,m,ans=1;
ull base1=131,base2=789,mol1=19260817,mol2=19660813;
char s[maxn];
struct Node{
	ull x,y;
}a[maxn];
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
	return s*w;
}
ull hash1(char s[]){
	ull anss=0;
	int len=strlen(s);
	for(int i=0;i<len;i++){
		anss=(anss*base1+(ull)s[i])%mol1;
	}
	return anss;
}
ull hash2(char s[]){
	ull anss=0;
	int len=strlen(s);
	for(int i=0;i<len;i++){
		anss=(anss*base1+(ull)s[i])%mol2;
	}
	return anss;
}
bool cmp(Node A,Node B){return A.x<B.x;}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		cin>>s;
		a[i].x=hash1(s);
		a[i].y=hash2(s);
	}
	sort(a+1,a+n+1,cmp);
	for(int i=2;i<=n;i++){
		if(a[i-1].x!=a[i].x||a[i-1].y!=a[i].y)ans++;
	}
	cout<<ans;
}

处理所有子串

不更了

posted @ 2020-06-29 16:54  _乀aakennes  阅读(369)  评论(0编辑  收藏  举报
levels of contents