Luogu P3370 [Template] String hash

Portal

Title description

As the title, given NNN strings (theiiThe length ofistrings isM i M_iMi , The string contains numbers, uppercase and lowercase letters, and is case sensitive), request how many different strings are in the NN strings.

Friendly reminder: If you really want to practice hashing, please be conscious, otherwise please turn right to PJ Proving Ground :)

Input format

The first line contains an integer NNN is the number of strings.
NextNNEach line of N lines contains a string, which is the string provided.

Output format

The output contains a line containing an integer, which is the number of different strings.

Input and output sample
input

5
abc
aaaa
abc
abcc
12345

Output

4

Let's first look at this hash constant ppWhat is the minimum p : 26 lowercase letters ('a' ~'z'), 26 uppercase letters ('A' ~'Z'), 10 numbers ('0'~'9'), which add up to a total 62 characters.

But to be on the safe side, it is best to set a slightly larger prime number, such as 137.

Next, we calculate the hash value of each string and find out if there are any duplicates.

#include <bits/stdc++.h>
using namespace std;
#define MAXN 10005
const int p = 137;        //初始化哈希常数
char s[MAXN];
#define LL long long int
LL h[MAXN];
map<LL, LL> m;            //这里的map是用来判断哈希值是不是重复的,如果重复了
LL gethash(char s[])
{
    
    
	int len = strlen(s);
	LL sum = 0;
	for(int i = len - 1; i >= 0; i--)
		sum = sum * p + s[i] - '0';        //哈希函数,我把一个字符串看作一137进制数处理了
	return sum;
}
int main()
{
    
    
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
	{
    
    
		scanf("%s", s);
		h[i] = gethash(s);
	}
	int ans = 0;
	for(int i = 1; i <= n; i++)
	{
    
    
		if(!m[h[i]])
		{
    
    
			m[h[i]] = 1;     //判断哈希值是否重复,其实双哈希最保险,但是我写的单哈希
			ans++;
		}
	}
	cout << ans << endl;
	return 0; 
}

Guess you like

Origin blog.csdn.net/CoderZeng/article/details/109065439