luogu3370 【模板】字符串哈希

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef unsigned long long ull;
struct Node{
    ull val1, val2;
}node[10005];
char a[1505];
int n, len, ans=0;
const ull base=131;
const ull mod1=19260817;
const ull mod2=1e9+7;
ull ha1(){
    ull x=0;
    for(int i=1; i<=len; i++)
        x = (x * base + (ull)a[i]) % mod1;
    return x;
}
ull ha2(){
    ull x=0;
    for(int i=1; i<=len; i++)
        x = (x * base + (ull)a[i]) % mod2;
    return x;
}
bool cmp(Node x, Node y){
    return x.val1<y.val1;
}
int main(){
    cin>>n;
    for(int i=1; i<=n; i++){
        scanf("%s", a+1);
        len = strlen(a+1);
        node[i].val1 = ha1();
        node[i].val2 = ha2();
    }
    sort(node+1, node+1+n, cmp);
    for(int i=1; i<=n; i++)
        if(node[i].val1!=node[i-1].val1 || node[i].val2!=node[i-1].val2)
            ans++;
    cout<<ans;
    return 0;
} 

set模板题

#include <iostream>
#include <string>
#include <set>
using namespace std;
set<string> s;
string a;
int n;
int main(){
    cin>>n;
    while(n--){
        cin>>a;
        s.insert(a);
    }
    cout<<s.size();
    return 0;
}
原文地址:https://www.cnblogs.com/poorpool/p/7966768.html