【模板】字符串哈希

OJ题号:洛谷3370

思路:BKDR

 1 #include<cstdio>
 2 #include<vector>
 3 #include<cstring>
 4 #include<algorithm>
 5 const unsigned int size=1000000007,M=1501;
 6 int BKDR_hash(char *s) {
 7     const unsigned int seed=1331;
 8     unsigned int hash=0;
 9     for(unsigned int i=0;i<strlen(s);i++) {
10         hash=(hash*seed+s[i])%size;
11     }
12     return hash;
13 }
14 int main() {
15     int n;
16     scanf("%d",&n);
17     std::vector<unsigned int> v;
18     while(n--) {
19         char s[M];
20         scanf("%s",s);
21         v.push_back(BKDR_hash(s));
22     }
23     std::sort(v.begin(),v.end());
24     printf("%u\n",std::unique(v.begin(),v.end())-v.begin());
25     return 0;
26 }

 

posted @ 2017-06-11 19:36  skylee03  阅读(100)  评论(0编辑  收藏  举报