-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path散列
41 lines (34 loc) · 1.06 KB
/
散列
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
//运用哈希时,将哈希函数封装成一个函数,并在输入数据时直接转化数据映射到哈希数组内
//整数哈希
#include<cstdio>
const int maxn=10000;
int hashtable[maxn]={0};
int main(void){
int n,m,x;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%d",&x);
hashtable[x]++;
}
for(int i=0;i<m;i++){
scanf("%d",&x);
printf("%d",hashtable[x]);
}
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//字符串哈希
#include<cstdio>
int hashFunc(char S[],int len){
int id=0;
for(int i=0;i<len;i++){
if(S[i]>='A'&&S[i]<='Z')
id=id*62+S[i]-'A';
else if(S[i]>='a'&&S[i]<='z')
id=id*62+S[i]-'z';
else if(S[i]>='0'&&S[i]<='9')
id=id*62+S[i]-'0';
}
return id;
}
------------------------------------------------------------------------------------------------------------------------------------------------------