字符串哈希 模板

最近看了下字符串哈希的算法,学习了大佬的博客,这里小结一下。
顺便附上大佬博客的链接,方便日后回顾。

  1. 字符串哈希函数总结
  2. wiki oi—字符串哈希
  3. 如何解决哈希冲突—暴雪的哈希算法

哈希:
我的理解是将字符当作某一进制的数来看,这样相同的字符串就会有一样的值,不相同的字符串的值就不同。但值得注意的是这个进制必须大于128,而且为减少哈希冲突,需要构建不同的哈希函数。

记录下我学的在竞赛中比较实用的一种,因为好记且比较短。


// BKDR Hash Function
unsigned int get_hash(const  string s)
{
	unsigned int res = 0;//哈希的结果
	unsigned int base = 131;//将字符串当作131进制的数
	for (int i = 0; i < s.length(); i++)
	{
		res = res * base + s[i];
	}
	return res&0x7fffffff;//与运算,使得所得结果为int
}

字符串哈希 模板_第1张图片

栗子:

Snowflake 题目描述
You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.
输入描述:
The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.
输出描述:
If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.
示例1
输入
复制
2
1 2 3 4 5 6
4 3 2 1 6 5
输出
复制
Twin snowflakes found.

#include 
#include 
#include 
using namespace std;
const int N =100010;
int p=99991,snow[N][6],head[N],ne[N],tot;
int h(int *a)
{
    int sum =0,mul =1;
    for(int i=0;i<6;i++)
    {
        sum = (sum+a[i])%p;
        mul = (long long)mul*a[i]%p;
    }
    return (sum+mul)%p;
}
bool equal(int*a,int *b)
{
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<6;j++)
        {
            bool eq =1;
            for(int k=0;k<6;k++)
                if(a[i+k%6]!=b[(j+k)%6])eq=0;
            if(eq)return 1;
            eq=1;
            for(int k=0;k<6;k++)
                   if(a[i+k%6]!=b[(j-k+6)%6])eq=0;
           if(eq)return 1;
           eq=1;
        }
    }
    return 0;
}
 
bool insert(int *a)
{
    int val = h(a);
    for(int i=head[val];i;i=ne[i])
        if(equal(snow[i],a))return 1;
    ++tot;
    memcpy(snow[tot],a,6*sizeof(int));
    ne[tot]=head[val];
    head[val]=tot;
    return 0;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int a[10];
        for(int j =0;j<6;j++)
            cin>>a[j];
        if(insert(a))
        {
            puts("Twin snowflakes found.");
            return 0;
        }
        
    }
     puts("No two snowflakes are alike.");
     
    return 0;
}

题目链接

字符串哈希 模板_第2张图片

你可能感兴趣的:(基本算法,题解)