进制哈希

例题

洛谷3370
https://www.luogu.com.cn/problem/P3370

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
#include<bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;

ull v[10010];
int n;
int P = 131; //进制

int fun(string s){
ull H = 0; //开ull溢出无所谓
for(int i = 0; i < s.size(); i++){
H = H*P + (s[i] - 'a')*P;
}
return H;
}

int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
string tmp;
for(int i = 0; i < n; i++){
cin>>tmp;
v[i] = fun(tmp);
}
sort(v, v + n);
int ans = 0;
for(int i = 1; i < n; i++){
if(v[i] == v[i-1]) ans++;
}
cout<<n-ans;
return 0;
}
  • Copyrights © 2023-2024 Kano
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信