81: luogu3370 hash

hash 模板题

#include <bits/stdc++.h>

using namespace std;

#define ULL unsigned long long
const ULL Mod = 212370440130137957ll;

char c[10010];
int pri = 233317;
ULL number[10010];

inline ULL Hash1(char s[]) {
    int Len = strlen(s);
    ULL base = 111, ret = 0;
    for(int i = 0; i < Len; i ++) ret = (ret * base + s[i]) % Mod + pri;
    return ret;
}

inline ULL Hash2(char s[]) {
    int Len = strlen(s);
    ULL base = 131, ret = 0;
    for(int i = 0; i < Len; i ++) ret = (ret * base + (ULL)s[i]) % Mod + pri;
    return ret;
}

inline ULL Hash3(char s[]) {
    int Len = strlen(s);
    ULL base = 4571, ret = 0;
    for(int i = 0; i < Len; i ++) ret = (ret * base + s[i]) % Mod + pri;
    return ret;
}

int main() {
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++) {
        scanf("%s", c);
        ULL Num = (((Hash1(c) * 123) % Mod * Hash2(c) * 345 % Mod) * Hash3(c)) % Mod;
        number[i] = Num;
    }
    sort(number + 1, number + n + 1);
    int tot = 1;
    for(int i = 1; i < n; i ++) if(number[i] != number[i + 1]) tot ++;
    cout << tot;
    
    return 0;
}

 

posted @ 2018-10-26 21:12  xayata  阅读(149)  评论(0编辑  收藏  举报