哈希表存储结构以及字符串哈希


一.哈希表存储结构

1 .拉链法

基本思想:
把具有相同散列地址的记录放在一个单链表中,称为同义词链表,有m个散列地址就有m个单链表,用一个一维数组h[]来存放每个链表的头指针,凡是散列地址为i的记录将其插入到以h[i]为头结点的单链表中。数组长度置为大于题目中所给范围的最小质数。

判断是否为质数:

bool is_prime(int n)//判断是否为质数
{
	if(n<2) return false;
	for(int i=2;i<=n/i;i++)
	{
		if(n%i==0) return false;
	}
	return true;
}

基本操作:

1.向散链表中插入一个数

void insert(int x)
{
	int k = (x % N + N) % N;//构造散列函数,求出散列地址,将x映射到0到N-1的存储单元中
	e[idx] = x;
	ne[idx] = h[k];
	h[k] = idx++;//将x插入到以h[k]为头结点的单链表中
}

2.在散链表查询某个数是否存在

bool find(int x)
{
	int k = (x % N + N) % N;
	for (int i = h[k];i != -1; i = ne[i])
	{
		if (e[i] == x) return true;
	}
	return false;
}

代码实例:

输入:
5
I 1
I 2
I 3
Q 2
Q 5
输出:

yes
no

#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef pair<int, int> PII;
typedef long long ll;
const int N = 100003;//尽可能把数组长度置为质数
int n,idx;//idx表示当前用到了哪个点
int h[N];//存储每个链表的头指针,用数组模拟链表,将其初始化为-1
int e[N];//存储每个结点的值
int ne[N];//存储每个结点的下一个位置
void insert(int x)
{
	int k = (x % N + N) % N;//构造散列函数,求出散列地址,将x映射到0到N-1的存储单元中
	e[idx] = x;
	ne[idx] = h[k];
	h[k] = idx++;//将x插入到以h[k]为头结点的单链表中
}
bool find(int x)
{
	int k = (x % N + N) % N;//求出散列地址
	for (int i = h[k];i != -1; i = ne[i])//遍历以h[k]为头结点单链表,判断是否存在
	{
		if (e[i] == x) return true;
	}
	return false;
}
int main()
{
	scanf("%d",&t);
	memset(h, -1, sizeof(h));//将每个链表头指针置为-1,表示为空
	while (n--)
	{
		char op[2];
		int x;
		scanf("%s%d", op,&x);//作为字符串用scanf进行读入,可以过滤空格和回车以及制表符
		if (*op == 'I') insert(x);
		else
		{
			if (find(x)) cout << "Yes" << endl;
			else cout << "No" << endl;
		}
	}
	return 0;
}

2. 开放地址法

基本思想:

相比于拉链法,此方法只需要开一个一维数组,数组的长度置为大于题目中所给范围的2到3倍的最小质数,初始化时,将数组的所有元素的置为不在题目中所给数据范围内的数,可将其置为0x3f3f3f3f,以此来表示此位置上为空。

核心操作:find()函数

步骤:

先求出x对应的散列地址,只要该位置上不为空并且该位置上的值不等于x的话,那么就往后进行查找,如果到末尾也没有查找的话,就从头开始查找。返回值有两种,当查找成功的话,函数返回x在散列表中的位置,查找失败的话,函数返回x在散列表中应该插入的位置。
int find(int x)//核心操作,如果查找成功的话,函数返回x在散列表中的位置,否则函数返回x在散列表中应该插入的位置

int find(int x)
{
	int k=(x%N+N)%N;//构造散列函数,求出x的散列地址
	while(h[k]!=null&&h[k]!=x)//只要该位置上有人并且该位置上的值不等于x的话,就继续往后查找,k++
	{
		k++;
		if(k==N) k=0;//如果查找到最后也没有找到的话,就从头开始查找,k=0
	}
	return k;
}

初始化操作
const int N = 200003,null=0x3f3f3f3f;//数组长度置为大于题目所给数据范围两倍的最小质数
//null是一个大于10的九次方的数,以此表示此位置上为空
memset(h, 0x3f, sizeof(h));

代码实例:

#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<map>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 200003, null = 0x3f3f3f3f;
const double eps=1e-6;
const int mod=1e9+7;
int n;
int h[N];//N的范围置为题目所给范围2~3倍的最小质数
int find(int x)//核心操作,如果查找成功的话,函数返回x在散列表中的位置,否则函数返回x在散列表中应该插入的位置
{
	int k=(x%N+N)%N;//构造散列函数,求出x的散列地址
	while(h[k]!=null&&h[k]!=x)//只要该位置上有人并且该位置上的值不等于x的话,就继续往后查找,k++
	{
		k++;
		if(k==N) k=0;//如果查找到最后也没有找到的话,就从头开始查找,k=0
	}
	return k;
}
int main()
{
	scanf("%d",&n);
	memset(h,0x3f,sizeof h);//初始化操作,将数组中的所有元素置为null,表示该位置上为空
	while(n--)
	{
		char op[2];
		int x;
		scanf("%s%d",op,&x);
		if(*op=='I') 
		{
			int k=find(x);
			h[k]=x;
		}
		else 
		{
			int k=find(x);
			if(h[k]!=null)  cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}

二. 字符串哈希方式

1.字符串前缀哈希法(常规)

算法应用范围:
当需要快速的判断的两个字符串是否相等的时候可以采用这个做法,时间复杂度比为O(1),比较快;
基本思想:
将字符串看成P进制数,P的经验值置为131或者13331,这样可以使冲突概率比较小。
小技巧:取模的数用2^64,可以直接用unsigned long long存储,溢出的结果就是取模的结果。

	//初始化
	p[0]=1;
	for(int i=1;i<=n;i++)
	{
		p[i]=p[i-1]*P;
		h[i]=h[i-1]*P+str[i];
	}

//计算字符串str[l~r]的哈希值
ULL get(int l,int r)
{
return h[r]-h[l-1]*p[r-l+1];
}
代码实例:

输入:
8 3
aabbaabb
1 3 5 7
1 3 6 8
1 2 1 2
输出: yes no yes

#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ULL;
const int N =1e6+10,null=0x3f3f3f3f;
const int P=131;
int n, m;
char str[N];//存储字符串,由于要使下标从一开始,所有下面读入的时候将起始地址加上一
ULL h[N];//h[k]存储字符串前k个字母的哈希值
ULL p[N];//p[k]存储p^kmod2^64
//计算字符串str[l~r]的哈希值
ULL get(int l,int r)
{
	return h[r]-h[l-1]*p[r-l+1];
}
int main()
{
		scanf("%d%d%s",&n,&m,str+1);
		//初始化
		p[0]=1;
		for(int i=1;i<=n;i++)
		{
			p[i]=p[i-1]*P;
			h[i]=h[i-1]*P+str[i];
		}
		
		while(m--)
		{
			int l1,r1,l2,r2;
			scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
			if(get(l1,r1)==get(l2,r2)) puts("Yes");//如果l1~r1之间的字符和l2~r2之间的字符哈希值相同的话,
			else puts("No");						//说明这两个区间的字符完全相同
		}
		return 0;
}

2.采用unordered_set

**注意:**内部含有count函数,因此可以知道某个元素出现的个数
解题思路:

由于题目要求求一个最小的数k,使得字符串中所有长度为k的子串均不相同,可以假设当k等于k0的时候,字符串中所有长度为k0的子串均不相同,可以发现,当k大于k0的时候,字符串中所有长度为k的子串均不相同,最小值k存在二段性,因此可以采用二分的思想进行解决。在判断字符串中所有长度为mid的子串是否均不相同的时候,可以采用哈希表unorded_set来存放所有的子串,枚举字符串的所有起点i,则终点为i+mid-1且终点小于字符串的长度,每次截取从起点i开始的长度为mid的子串,然后判断该子串是否之前出现过,如果出现过的话,说明存在相同的子串,不符合题意,函数返回false,否则将当前取出的子串存入到哈希表中,循环结束之后,字符串中所有长度为mid的子串均不相同,函数返回true。

代码实例:

输入:7
ABCDABC
输出:4

#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e5+10, null = 0x3f3f3f3f;
const double eps=1e-6;
int n;
string s;
bool check(int mid)//判断字符串中所有长度为mid的子串是否均不相同,是的话返回true,否则返回false
{
	unordered_set<string> hash;//存储每次当前遇到的字符串中的字串
	for(int i=0;i+mid-1<s.size();i++)//枚举每个子串的起点,起点为i,则终点为i+mid-1,切终点范围不能超过字符串的长度
	{
		string ans=s.substr(i,mid);//每次从起点i开始,取出长度为mid的字串
		if(hash.count(ans)) return false;//如果这个子串之前出现过,说明含有重复的子串,不符合题意
		hash.insert(ans);//将每次取出的子串存放在哈希表hash中
	}
	return true;//如果取出的子串之前都没有出现过,说明字符串中所有长度为mid的子串均不相同,函数返回true,符合题意
}
int main()
{
	cin>>n>>s;
	int l=1,r=n;//目标值最小为1,最大为n
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid)) r=mid;//如果字符串中长度为mid的子串均不相同的话,说明mid符合题意,因为要求最小的,因此目标值在左边
		else l=mid+1;
	}
	cout<<l<<endl;//此时输出l或者r都一样,因为当循环结束的时候,l和r相同
	return 0;
}

3.采用stl库实现的哈希表unordered_map(查询效率非常快)

代码实例:

#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e5+10, null = 0x3f3f3f3f;
const double eps=1e-6;
int n;
string s;
bool check(int mid)//判断字符串中所有长度为mid的子串是否均不相同,是的话返回true,否则返回false
{
	unordered_map<string,int> hash;//存储每次当前遇到的字符串中的字串
	for(int i=0;i+mid-1<s.size();i++)//枚举每个子串的起点,起点为i,则终点为i+mid-1,切终点范围不能超过字符串的长度
	{
		string ans=s.substr(i,mid);//每次从起点i开始,取出长度为mid的字串
		if(hash[ans]) return false;//如果这个子串之前出现过,说明含有重复的子串,不符合题意
		hash[ans]++;//将每次取出的子串存放在哈希表hash中
	}
	return true;//如果取出的子串之前都没有出现过,说明字符串中所有长度为mid的子串均不相同,函数返回true,符合题意
}
int main()
{
	cin>>n>>s;
	int l=1,r=n;//目标值最小为1,最大为n
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid)) r=mid;//如果字符串中长度为mid的子串均不相同的话,说明mid符合题意,因为要求最小的,因此目标值在左边
		else l=mid+1;
	}
	cout<<l<<endl;//此时输出l或者r都一样,因为当循环结束的时候,l和r相同
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值