【C/C++】散列/算法笔记4.2

先说一下我自己的理解。
我先给你N组数据,这个N组里可能有重复的!
然后我们先统计好了N组里面的独立的每个对应的出现了几次(相当于map,然后每项属性有出现了多少次的),用的是数组下标对应
现在我们给了M个名字,要看这个出现了几次
就直接返回数组下标对应的数
但是呢,这个数组可能会太长了,我们就用一些方法,比如求余,让这组数尽量紧凑起来。
但是求余会导致得到同样的结果,处理的方法有H(key)+1,+2..或者H(key)+1^2 再或者用一个链表,把H(key)相同的存起来。

  1. 散列的定义与整数散列
    给定数组N和数组M,要查询M中的数字是否在N中出现过,用空间换时间,建立一个哈希表,如果被查询数组N中有这个数字x,就将哈希表的第x个bool值改为true,不然就是false。
    然后一边读入M中的数字,一边看哈希表中对应位置是不是为true,如果是就输出。
#include <iostream>
const int maxn = 100000;
bool hashtable[maxn] = {false}; //一次将所有的置为false

int main()
{
   // for (int i = 0; i < 10; i++)
   // {
   //    if (hashtable[i] == false)
   //    {
   //       printf("Yes
");
   //    }
   // }
   int m, n, x;
   scanf("%d%d", &n, &m);
   for (int i = 0; i < n; i++)
   {
      scanf("%d", &x);
      hashtable[x] = true;
   }
   for (int i = 0; i < m; i++)
   {
      scanf("%d", &x);
      if (hashtable[x] == true)
      {
         printf("%d
",x);
      }
   }
   system("pause");
}

如果要看查询表M中的元素,在被查询N数组中出现的次数,把hashtable设置为int型。

#include <iostream>
const int maxn = 100000;
int hashtable[maxn];

int main()
{
   memset(hashtable,0,sizeof(hashtable));
   int m, n, 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]);
   }
   system("pause");
}

2.字符串hash
字母用用把字母转化到十进制数,然后再用1的方法。
给出N个字符串统计,给出M个查询串,问每个查询串在N个字符串种出现的次数。
hashtable是262626,因为26进制转化后最大为(26^2+26+1)*25(注意是25),变化为1,开始为000.

#include <iostream>
#include <string.h>
using namespace std;
const int maxn = 100; //设置被查询串个数
char s[maxn][5]; //字典,3位大写英文,有maxn组
char temp[5];
int hashtable[26*26*26 + 10]; 

int hashFunc(char a[], int len)
{
   int id = 0;
   for (int i = 0; i < len; i++)
   {
      id = (a[i] - 'A') + id*26;
   }
   return id;
}

int main()
{
   memset(hashtable,0,sizeof(hashtable));
   //输入输出
   int m, n; //m个查询串,n个被查询串(字典)
   scanf("%d%d", &m, &n);
   //建立字典
   for (int i = 0; i < n; i++)
   {
      scanf("%s", s[i]);
      hashtable[hashFunc(s[i], 3)]++;
   }

   for (int i = 0; i < m; i++)
   {
      scanf("%s", temp);
      int t = hashFunc(temp, 3);
      printf("%d
", hashtable[t]);
   }
   system("pause");
}
原文地址:https://www.cnblogs.com/kinologic/p/14155472.html