散列函数的应用

散列

前身:对于在M个预查询的数中每个数在N个数中出现的次数类型题目,空间换时间的方法来减少时间复杂度,用一个标记函数标记好M中每个元素,然后在N中输入时,直接把输入的数作为标记函数的下标对这个数的性质进行统计。

散列的定义:

将元素通过一个函数(散列函数)转换为整数,使得该整数可以唯一地表示这个元素。

即将key 通过散列函数 转化为 整数
key的类型的不同,导致散列函数的不同。

当key 是整数时
  • 直接定地址法:即直接将H(key) = key; //最常见并且最实用的散列应用
    -线性变换:H(key) = a*key + b;
  • 平方取中法:key的平方的中间若干位作为Hash值
  • 除留余数法:H(key)=key %mod ; //较为常用.
    注意:表长Tsize必须不小于mod,不然会产生越界;并且mod为素数时,H(key)能尽可能覆盖到 [0,mod]范围内的每个数。
    但是这种方法有一个不可避免的冲突,即可能发生H(key1) 与 H(key2) 是相同的情况。
    决绝方法:
    • 1.线性探查法:
      如果在表中下标为H(Key2)的位置已经被H(key1)占据了,那么检查下一个位H(key2)+1 是否被占据,如果没有,就使用这个位置;否则继续循环不断加一,直到找到可以使用的为止。
    • 2.平方探查法
      顺序检查表中位置:H(key)+12、H(key)-12、H(key)+22H(key)-22……。如果H(key)+K2超过了表厂TSize,那么就把H(Key)+k2对表长取模。
当key 是字符串时

方法一:
先将字符型转化为整型:

int hashchange(char t){  //PAT中1084,题目中需要转化'A'~'Z','a'~'z','_',其中忽略大小写,那么转化的时候只需要看成0-36个数
	if(t<='Z'&&t>='A'){
		return  t-'A';
	} 
	else if(t<='z'&&t>='a'){
		return t-'a';
	} 
	else if(t == '_'){
		return 36;
	}
	else{
		return 26+t-'0';
	}
}

然后依次标记好转为整型的hashlabel[hashchange(s)] = true;

方法二:
或者对字符型数据进行标记:
其中对hashlabel初始化为: bool hashlabel[128] = {false}
然后对每一个已经存在的数据进行标志。
具体可以看例题2哦。

例题解析

试题 PAT 1092 To Buy or Not to Buy (20分)

Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is Yes, please tell her the number of extra beads she has to buy; or if the answer is No, please tell her the number of beads missing from the string.

For the sake of simplicity, let’s use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. For example, the 3rd string in Figure 1 is the one that Eva would like to make. Then the 1st string is okay since it contains all the necessary beads with 8 extra ones; yet the 2nd one is not since there is no black bead and one less red bead.

Figure 1

Input Specification:
Each input file contains one test case. Each case gives in two lines the strings of no more than 1000 beads which belong to the shop owner and Eva, respectively.

Output Specification:
For each test case, print your answer in one line. If the answer is Yes, then also output the number of extra beads Eva has to buy; or if the answer is No, then also output the number of beads missing from the string. There must be exactly 1 space between the answer and the number.

Sample Input 1:
ppRYYGrrYBR2258
YrR8RrY
Sample Output 1:
Yes 8
Sample Input 2:
ppRYYGrrYB225
YrR8RrY
Sample Output 2:
No 2

题意: ⼩红想买些珠⼦做⼀串⾃⼰喜欢的珠串。卖珠⼦的摊主有很多串五颜六⾊的珠串,但是不肯把任何⼀串拆散了卖。因此需要帮她解决,她有多少多余的珠⼦;如果不是,那么告诉她缺了多少珠⼦~

代码
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int hashchange(char t){
	if(t<='Z'&&t>='A') return t-'A';
	else if(t<='z'&&t>='a') return 26+t-'a';
	else  return 52+t-'0';
} 
int hashlabel[128] = {0};
string a,b;
int main(){
	cin>>a;
	cin>>b;
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	int id;
	for(int i=0;i<b.size();i++){
		id = hashchange(b[i]);
		hashlabel[id] += 1;
	}
	int extra = 0;
	for(int i=0;i<a.size();i++){
		id = hashchange(a[i]);
		if(!hashlabel[id]) extra++;
		else{
			hashlabel[id]--;
		}
	}
	int lack = hashlabel[hashchange(b[0])];
	for(int i=1;i<b.size();i++){
		id = hashchange(b[i]);
		if(id == hashchange(b[i-1])) continue;
		lack += hashlabel[id];  
	}
	if(lack) printf("No %d\n",lack);
	else printf("Yes %d\n",extra);
	return 0;
}

试题 PAT1050 String Subtraction (20分)

Given two strings S​1 and S​2 , S=S​1 −S​2​​ is defined to be the remaining string after taking all the characters in S2 from S​1​​ . Your task is simply to calculate S​1​​ −S​2 for any given strings. However, it might not be that simple to do it fast.

Input Specification:
Each input file contains one test case. Each case consists of two lines which gives S1 ​​ and S​2 ​​ , respectively. The string lengths of both strings are no more than 104 . It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.

Output Specification:
For each test case, print S​1−S​2 in one line.

Sample Input:
They are students.
aeiou
Sample Output:
Thy r stdnts.
题意:给出两个字符串,在第⼀个字符串中删除第⼆个字符串中出现过的所有字符并输出

代码
#include <iostream>
#include <string>
using namespace std;
string s1,s2;
bool hashlabel[128] = {false};

int main(){
	getline(cin,s1);
	getline(cin,s2);
	for(int i=0;i<s2.size();i++){
		hashlabel[s2[i]] = true;
	}
	for(int i=0;i<s1.size();i++){
		if(!hashlabel[s1[i]]) cout<<s1[i]; 
	}
	cout<<endl;
	return 0;
}

试题 PAT 乙级1005 继续(3n+1)猜想 (25分)

卡拉兹(Callatz)猜想:
对任何一个正整数 n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把 (3n+1) 砍掉一半。这样一直反复砍下去,最后一定在某一步得到 n=1。
当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。例如对 n=3 进行验证的时候,我们需要计算 3、5、8、4、2、1,则当我们对 n=5、8、4、2 进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这 4 个数已经在验证3的时候遇到过了,我们称 5、8、4、2 是被 3“覆盖”的数。我们称一个数列中的某个数 n 为“关键数”,如果 n 不能被数列中的其他数字所覆盖。

现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。

输入格式:
每个测试输入包含 1 个测试用例,第 1 行给出一个正整数 K (<100),第 2 行给出 K 个互不相同的待验证的正整数 n (1<n≤100)的值,数字间用空格隔开。

输出格式:
每个测试用例的输出占一行,按从大到小的顺序输出关键数字。数字间用 1 个空格隔开,但一行中最后一个数字后没有空格。

输入样例:
6
3 5 6 7 8 11
输出样例:
7 6

代码
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n ,ipt[110];
int hashtable[10000] = {0}; 
int main(){
	scanf("%d",&n);
	int  t;
	for(int i=0;i<n;i++){
		scanf("%d",&ipt[i]);
		t = ipt[i];
		while(t!=1){
			if(t%2==0) t = t/2;
			else t = (t*3+1)/2;
			hashtable[t] = 1;				
		}
	}
	vector <int> ans;
	for(int i=0;i<n;i++){
		if(hashtable[ipt[i]]==0)  ans.push_back(ipt[i]);
	}	
	sort(ans.begin(),ans.end());
	for(int i=ans.size()-1;i>=0;i--){
		if(i!=ans.size()-1) cout<<" ";
		cout<<ans[i];
	}
	return 0;
}

勉励
打卡第十五天,加油ヾ(◍°∇°◍)ノ゙
今日感悟:不喜形于色,不溢于言表

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值