[Ybtoj high-efficiency advanced 2.2] [hash] string hash

[Ybtoj high-efficiency advanced 2.2] [hash] string hash

topic

Insert picture description here
Insert picture description here


Problem-solving ideas

hash template


Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int e = 131;
const long long mo = 200709081011;
long long n, a[10010];
string s;
long long hash(string x) {
    
    
    long long tot = 0;
    for (int i = 0; i < x.size(); i++) tot = (tot * e + x[i]) % mo;
    return tot;
}
int main() {
    
    
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++) {
    
    
        cin >> s;
        a[i] = hash(s);
    }
    sort(a + 1, a + n + 1);
    int i = 1, x = n;
    while (i < x) {
    
    
        while (a[i] == a[i + 1] && i < x) {
    
    
            n--;
            i++;
        }
        i++;
    }
    printf("%lld\n", n);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/113362963