字符串哈希【Hash】

>Link

ybtoj字符串哈希


>解题思路

一道哈希模板题,其实就是这道题
要注意一下hash值的定义和模数的选择


>代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int p = 1e8 + 7;
string a;
int n, ans;
bool v[p];

int main()
{
    
    
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++)
	{
    
    
		cin >> a;
		int H = 0, len = a.size();
		for (int j = 0; j < len; j++)
		  H = (H * 10 + a[j]) % p;
		if (!v[H]) ans++, v[H] = 1;
	}
	printf ("%d", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43010386/article/details/114433324