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);
    }
}
时间: 2024-12-13 11:04:44

hash 【模板】的相关文章

字符串HASH模板 取自挑战程序设计竞赛(第2版)

/*===================================================* 从b串中寻找和a串长度相同的子串,返回开始位置 不保证绝对正确,发生冲突概率为O(sqrt(n)), n为哈希函数的最大值 \*===================================================*/ #define ull unsigned long long const ull B = 1e8+7; /*according to the book*/

hash 模板

首先,附上hash完整模板,分析见:http://www.cnblogs.com/wsdestdq/p/6831041.html 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 typedef long long LL; 7 8 int n; 9 const int D = 131; 10 const int MD = 1e9 + 7; 11 i

hash 模板 x

不懂hash是什么的盆友给出直通车:滴滴滴,开车啦~ 如果你看懂了的话: hash模板来也~ #include <cstdio> #include <string> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e5 + 6; //10006 const int D = 131; //质数 const int

线段树、KMP、HASH模板

线段树 #include<cstdio> using namespace std; int n,p,a,b,m,x,y,ans; struct node { int l,r,w,f; }tree[400001]; inline void build(int k,int ll,int rr)//建树 { tree[k].l=ll,tree[k].r=rr; if(tree[k].l==tree[k].r) { scanf("%d",&tree[k].w); retur

字符串Hash模板

1 unsigned int SDBMHash(char *str) { 2 unsigned int hash = 0; 3 while (*str) 4 // equivalent to: hash = 65599*hash + (*str++); 5 hash = (*str++) + (hash << 6) + (hash << 16) - hash; 6 7 return (hash & 0x7FFFFFFF); 8 } 9 10 // RS Hash Funct

poj 2002(好题 链式hash+已知正方形两点求另外两点)

Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 18493   Accepted: 7124 Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating abou

几道hash题

1: UVa 10887 - Concatenation of Languages map 可以做 ,但是输入实在恶心,有空串之类的HASH模板: int Hash(char *s){   int seed=131,sum=0;   while (*s)   sum=sum*seed+(*s++);   return (sum&0x7FFFFFFF)%N;} void hash_insert(int s){   int h=Hash(c[s]);   int u=head[h];   while

poj 3461 字符串单串匹配--KMP或者字符串HASH

http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <iostream> #include <cmath> #include <map> #include <queue> using namespace std;

P3370 【模板】字符串哈希

传送门 题目大意 求n个字符串不同的个数 题解 hash模板 1LL强制转换成long long 代码 #include<iostream> #include<algorithm> #include<cstdio> #include<set> #include<cstring> using namespace std; //#define mod 1e9+7//不能这样宏定义 const int mod=1e9+7; #define D 131

hdu5183 hash大法

维护前缀和sum[i]=a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i]枚举结尾i,然后在hash表中查询是否存在sum[i]-K的值.如果当前i为奇数,则将sum[i]插入到hash表中.上面考虑的是从i为偶数为开头的情况.然后再考虑以奇数开头的情况,按照上述方法再做一次即可.不同的是这次要维护的前缀和是sum[i]=-(a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i])I为偶数的时候将sum[i]插入到hash表.总复杂度o(n) 注意一个tips:sca