Only discuss the most common binary hash.
example
Meaning of the questions: How many seek different string ($ N \ leq 10 ^ 4 $) N string there.
analysis:
Seeking a hash value for each string, how many different hash values are then statistically there.
Single Hash
Select a large prime number, or natural overflow.
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const ull base = 233; ull mod=212370440130137957ll; const int maxn = 10000+10; int n; wool f [maxn]; char s[maxn]; the Hash ULL ( char S []) // Nature spill { wool ans = 0 ; int len = strlen (S); for ( int i = 0 ; i <len; i ++ ) { ans = ( base * + ANS (ULL) s [i])% v; } return years; } set<ull>st; int main() { scanf("%d", &n); for(int i = 0;i < n;i++) { scanf("%s", s); st.insert(Hash(s)); } printf("%d\n", st.size()); }
Double hashing
The use of two modular, hash collision probability will be much lower, but constant increases.
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const ull base = 233; wool mod1 = 212370440130137957ll; ull mod2 = 1 << 30; const int maxn = 10000+10; int n; char s[maxn]; Hashl ULL ( char S []) // Nature spill { wool ans = 0 ; int len = strlen (S); for ( int i = 0 ; i <len; i ++ ) { years = ( base * year + (ull) s [i])% mod1; } return years; } Hash2 ULL ( char S []) // Nature spill { wool ans = 0 ; int len = strlen (S); for ( int i = 0 ; i <len; i ++ ) { years = ( base * year + (ull) s [i])% mod2; } return years; } struct Node{ wool x, y; }f[maxn]; bool cmp(Node a, Node b) { if(a.x == b.x) return a.y < b.y; return a.x < b.x; } int main () { scanf("%d", &n); for(int i = 0;i < n;i++) { scanf("%s", s); f[i].x = Hash1(s); f[i].y = Hash2(s); //printf("%lld %lld\n", f[i].x, f[i].y); } sort(f, f+n, cmp); int cnt = 1; for(int i = 0;i < n-1;i++) { if(f[i+1].x != f[i].x || f[i+1].y != f[i].y) cnt++; } printf("%d\n", cnt); }
Reference Links: https://www.cnblogs.com/henry-1202/p/8868414.html