【ybtoj 高效进阶 2.2】【hash】 字符串哈希

【ybtoj 高效进阶 2.2】【hash】 字符串哈希

题目

在这里插入图片描述
在这里插入图片描述


解题思路

hash模板


代码

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

猜你喜欢

转载自blog.csdn.net/qq_45621109/article/details/113362963
今日推荐