字符串Hash - Luogu P3370

字符串Hash - Luogu P3370

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;

ull base = 131;
ull a[10010];
char s[10010];

int n;
int prime = 233317;
ull mod = 212370440130137957ll;

ull hash(char s[]){
    int len = strlen(s);
    ull ans = 0;
    for(int i = 0; i < len; ++i){
        ans = (ans * base + (ull) s[i]) % mod + prime;
    }
    return ans;
}


int main(){
    scanf("%d", &n);

    for(int i = 1; i <= n; ++i){
        scanf("%s", s);
        a[i] = hash(s);
    }

    sort(a+1,a+n+1);

    int ans = 1;

    for(int i = 1; i < n; ++i){
        if(a[i] != a[i+1]) ++ans;
    }

    printf("%d", ans);
    return 0;
}
posted @ 2021-05-10 10:26  popozyl  阅读(44)  评论(0编辑  收藏  举报