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

· · 个人记录

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

#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;
}