字符串hash模版
字符串hash模版
原题目:P3370 【模板】字符串哈希 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll seed=31,mod1=1e9+7,mod2=1e9+9,maxn=1e5+7;
int n;
struct hash_s{
ll hash1,hash2;
hash_s():hash1(0),hash2(0){}
const bool operator <(hash_s x)const{
if(hash1<x.hash1) return true;
else if(hash1==x.hash1 && hash2<x.hash2) return true;
return false;
}
const bool operator ==(hash_s x)const{
return hash1==x.hash1 && hash2==x.hash2;
}
}hashes[maxn];
hash_s f(string s){
hash_s ret;
for(int i=0;i<s.size();++i){
ret.hash1=(ret.hash1*seed+ll(s[i]))%mod1;
ret.hash2=(ret.hash2*seed+ll(s[i]))%mod2;
}
return ret;
}
int main(){
cin>>n;
for(int i=0;i<n;++i){
string ss;
cin>>ss;
hashes[i]=f(ss);
}
map<hash_s,int> mp;
int ans=0;
for(int i=0;i<n;++i){
if(mp[hashes[i]]==0){
ans++;
mp[hashes[i]]=1;
}
}
cout<<ans<<endl;
}