• 「字符串」哈希板子


    单哈希

    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;
    }
    

    处理所有子串

    不更了

  • 相关阅读:
    2017 ACM-ICPC西安网赛B-Coin
    Codeforces389D(SummerTrainingDay01-J)
    Codeforces672D(SummerTrainingDay01-I)
    VS2017.滚动条选项
    VS.自动调试
    ffmpeg.mp4.格式资料
    vs2017.unresolved external symbol __imp__fprintf&__imp____iob_func
    vs2017."const char *"的实参与"char *"的形参不兼容_goto跳过类型声明
    vue项目开发时怎么解决跨域
    vue奇怪的知识点又增加了
  • 原文地址:https://www.cnblogs.com/614685877--aakennes/p/13208832.html
Copyright © 2020-2023  润新知