码迷,mamicode.com
首页 > 其他好文 > 详细

hash 【模板】

时间:2017-08-16 09:54:30      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:clu   ace   一个   amp   pre   name   efi   height   mod   

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

 

hash 【模板】

标签:clu   ace   一个   amp   pre   name   efi   height   mod   

原文地址:http://www.cnblogs.com/CLGYPYJ/p/7371457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!