P3370 [模板] 字符串哈希

51 篇文章 1 订阅
4 篇文章 0 订阅

传送门
对于字符串的处理方式有很多,HASH算是其中的一种暴力方法。
思想:通过某种方式在字符串和数字之间建立某种 一 一 对应的关系
比如’abcde’就可以看成
这里写图片描述
base是我们自定的一个数,类似于数中的进制,取决于判断的字符串不同字符的个数。
但是我们这样算出的HASH值有可能爆类型,于是要取模
这样算出的HASH值有如下性质
1:如果两个字符串HASH值不同,那么两个字符串一定不同
2:如果两个字符串HASH值相同,那么两个字符串可能相同,也可能不同(因为取模辣)
这样为防止出现不同但是HASH值相同的情况,我们可以使用双模数甚至是三模数,也可以使用无符号的长整型(自动溢出取模),对于模数,尽量取较大的质数,以尽肯能减少出现不同但是HASH值相同的情况。
常用的有 1e9+7,1e9+9
单哈希

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
const LL D1=131;
const LL MOD1=1e9+7;
LL hash1[10010];
char ss[10010];
LL make_hash1(char s[])
{
    LL ans=0;
    for(int i=0;i<strlen(s);i++)
     ans=(ans*D1+(LL)(s[i]))%MOD1;
    return ans; 
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
     scanf("%s",ss),hash1[i]=make_hash1(ss);

    sort(hash1+1,hash1+n+1);
    int ans=1;
    for(int i=2;i<=n;i++)
     if(hash1[i]!=hash1[i-1]) ans++;
    printf("%d",ans);
    return 0; 
}

双哈希

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
const LL D1=131;
const LL D2=131;
const LL MOD1=1e9+7;
const LL MOD2=1e9+9; 
char ss[10010];
struct node{
    LL hash1,hash2;
}a[10011];
LL make_hash1(char s[])
{
    LL ans=0;
    for(int i=0;i<strlen(s);i++)
     ans=(ans*D1+(LL)(s[i]))%MOD1;
    return ans; 
}
LL make_hash2(char s[])
{
    LL ans=0;
    for(int i=0;i<strlen(s);i++)
     ans=(ans*D2+(LL)(s[i]))%MOD2;
    return ans; 
}
bool comp(const node&a,const node&b)
{
    return a.hash1<b.hash1;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
     scanf("%s",ss),a[i].hash1=make_hash1(ss),a[i].hash2=make_hash2(ss);

    sort(a+1,a+n+1,comp);
    int ans=1;
    for(int i=2;i<=n;i++)
     if(a[i].hash1!=a[i-1].hash1||a[i].hash2!=a[i-1].hash2) ans++;
    printf("%d",ans);
    return 0; 
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值