字符串Hash

字符串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;
}
---- suffer now and live the rest of your life as a champion ----
原文地址:https://www.cnblogs.com/popodynasty/p/14750012.html