跳到正文

个人记录

题解 P3370 【模板】字符串哈希

zimindaada约 1 分钟
原文
文章信息与版本

文章信息

发布
2020年1月18日
收录
2026年1月19日
更新
2026年11月13日
篇幅
约 1 分钟

版本

  1. v1当前

一道字符串哈希的模板题。这个AC代码用了ull的溢出,没有取模(实测用19260817取模只有80分,没有打双模数,懒)。

C++
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 10005;
const ull mod = 19260817;
const ull base = 101;
int n;
ull hash[maxn];
int ans;
int main(){
	scanf("%d",&n);
	char tmp[1505]; int len;
	ull num;
	for(int i = 1; i <= n; ++i){
		scanf("%s",tmp+1);
		len = strlen(tmp+1); num = 0;
		for(int j = 1; j <= len; ++j){
			num = (num*base + (tmp[j]-46));
		}
		hash[i] = num;
	}
	sort(hash+1,hash+n+1);
	for(int i = 1; i <= n; ++i){
		if(hash[i] ^ hash[i+1]) ++ans;
	}
	printf("%d\n", ans);
	return 0;
}
原文