P3370 【模板】字符串哈希

P3370 【模板】字符串哈希

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned long long llu;
 4 const int maxn = 10005;
 5 llu base = 131, mod = 212370440130137957ll;
 6 llu a[maxn];
 7 char s[maxn];
 8 int prime = 233317;
 9 llu hashe(char *s) {
10     int len = strlen(s);
11     llu ans = 0;
12     for (int i = 0; i < len; i++)
13         ans = (ans*base+(llu)s[i])%mod+prime;
14     return ans;
15 }
16 int main() {
17     int n; scanf("%d",&n);
18     for (int i = 1; i <= n; i++) {
19         scanf("%s",s);
20         a[i] = hashe(s);
21     }
22     sort(a+1,a+1+n);
23     int ans = 1;
24     for (int i = 1; i < n; i++) {
25         if (a[i] != a[i+1]) ans++;
26     }
27     printf("%d\n",ans);
28     return 0;
29 }

 

posted @ 2019-10-23 13:32  麻辣猪仔  阅读(171)  评论(0编辑  收藏  举报