哈希映射

Date:2019-07-18 19:58:48

  1 /*-------------------------------散列函数-------------------------------*/
  2 /*  3     1.直接定址法:H(key)= key
  4     2.线性变换法:H(key)= a*key + b
  5     3.平方取中法:H(key)= key^2 % 100000 / 1000 (取中间几位)
  6     4.除留余数法:H(key)= key % mod (mod取素数,确保能够映射至0~mod; Tsize >= mod)
  7 */
  8 
  9 /*-------------------------------冲突避免-------------------------------*/
 10 /* 11     1.开放定址法——线性探测法(Linear Probing):
 12         H(key)= H(key)+ 1
 13         if( H(key) > Tsize )
 14             H(key) = 0;
 15     2.开放地址法——平方探测法(Quadratic Probing):
 16         H(key)= H(key)+/- k^2 (k=1,2,3,...)
 17         if( H(key) > Tsize )
 18             H(key) %= mod;
 19         if( H(key) < 0 )
 20             H(key) = ( H(key)%Tsize+Tsize ) % Tsize        (不断+Tsize,至出现第一个正数)
 21     3.链地址法——拉链法:
 22         int h = H(key);
 23         typename *p = (typename*)malloc(sizeof(typename));
 24         p->data = key;
 25         p->next = NULL;
 26         link[h]->next = p;
 27 */
 28 
 29 /*-------------------------------整数散列-------------------------------*/
 30 #include <stdio.h>
 31 
 32 const int maxn = 100010;    //int ~ 10^9 ; long long ~ 10^18
 33 
 34 bool hash_table_1[maxn] = {false};    //是否出现
 35 int  hash_table_2[maxn] = {  0  };    //出现次数
 36 
 37 int main()
 38 {
 39     int n, m, x;
 40     scanf("%d %d", &n, &m);
 41 
 42     for(int i=0; i<n; i++)
 43     {
 44         scanf("%d", &x);
 45         hash_table[x] = true;
 46     }
 47 
 48     for(int i=0; i<m; i++)
 49     {
 50         scanf("%d", &x);
 51         if(hash_table[x] == true)
 52         {
 53             printf("YES\n");
 54         }
 55         else
 56         {
 57             printf("NO\n");
 58         }
 59     }
 60 
 61     return 0;
 62 }
 63 
 64 /*------------------------------------字符串散列-------------------------------*/
 65 #include <stdio.h>
 66 
 67 const int max_num = 100;
 68 const int max_siz = 26*26*26+10;
 69 
 70 char s[max_num][5], temp[5];
 71 int hash_table[max_siz];
 72 
 73 //把len长的大写字母,映射至唯一的整数(26进制-->10进制)
 74 int hash_func_1(char s[], int len)
 75 {
 76     int id = 0;
 77     for(int i=0; i<len; i++)
 78     {
 79         id = id*26 + (s[i]-'A');
 80     }
 81 
 82     return id;
 83 }
 84 
 85 //把len长的大小写字母,映射至唯一的整数(52进制-->10进制)
 86 int hash_func_2(char s[], int len)
 87 {
 88     int id = 0;
 89     for(int i=0; i<len; i++)
 90     {
 91         if(s[i]>='A' && s[i]<='Z')
 92         {
 93             id = id*52 + (s[i]-'A');
 94         }
 95         else
 96         {
 97             id = id*52 + (s[i]-'a') + 26;
 98         }
 99     }
100 
101     return id;
102 }
103 
104 //len长的大写字母加末尾一位数字,映射至唯一的整数
105 int hash_func_3(char s[], int len)
106 {
107     int id = 0;
108     for(int i=0; i<len-1; i++)
109     {
110         id = id*26 + (s[i]-'A');
111     }
112     id = id*10 + (s[len-1]-'0');
113 
114     return id;
115 }
116 
117 /*
118 给出N个字符串(恰好由三位大写字母组成),再给出M个查询字符串,
119 问每个查询字符串在N个字符串中出现的次数。
120 */
121 int main(void)
122 {
123     int n, m;
124     scanf("%d %d", &n, &m);
125 
126     for(int i=0; i<n; i++)
127     {
128         scanf("%s", s[i]);
129 
130         int id = hash_func_1(s[i], 3);
131         hash_table[id]++;
132     }
133 
134     for(int i=0; i<m; i++)
135     {
136         scanf("%s", temp);
137         int id = hash_func_1(temp, 3);
138 
139         printf("%d\n", hash_table[id]);        //输出该字符串出现的次数
140     }
141 
142     return 0;
143 }

 

posted @ 2019-07-18 20:00  林東雨  阅读(925)  评论(0编辑  收藏  举报