hash 【模板】

          hash

功能:  hash一般用于快速判断两个或多个字符串是否匹配。

实现 :    想一想,如果比较两个数子的话是很方便的很快,那么我们把整个字符串看成一个大数。

      它是base进制的len位数。但是我只会比较十进制啊,那就转成10进制,但又太大了(成百上千位啊)

     不得以,我只好请来一个大质数MOD把这些大数映射到一个较小的范围内比较。

代码:     

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<cstdio>
using namespace std;
#define LL unsigned long long 
#define MOD 1000000007
#define base 211
LL n,b[20000];
LL get_hash(char s[ ])
{
    int len=strlen(s);
    LL ans=0;
    for(int i=0;i<len;i++)
    ans=(ans*base + (LL) s[i])%MOD;
    return ans;
}
int main()
{
    scanf("%lld",&n);
    char s[60000];
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        b[i]=get_hash(s);
    }
}

 

转载于:https://www.cnblogs.com/CLGYPYJ/p/7371457.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值