0%

P3370 【模板】字符串哈希

思路讲解

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
const ll MAXN=static_cast<ll>(1e6)+19;
const ll mod=static_cast<ll>(1e18)+3,base=114;

ll N;
ll myhash[MAXN];

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>N;
for(int i=0;i<N;++i){
string s;
cin>>s;
ll val=0;
for(int j=0;j<s.size();++j){
val=(s[j]+val*base)%mod;
}
myhash[i]=val;
}
sort(myhash, myhash+N);
ll ans=0;
myhash[N]=-1;
for(int i=0;i<N;++i){
if(myhash[i]!=myhash[i+1]){
++ans;
}
}
cout<<ans<<'\n';
return 0;
}
// AC https://www.luogu.com.cn/record/198474085

心路历程(WA,TLE,MLE……)