您当前的位置: 首页 >  散列表

MangataTS

暂无认证

  • 0浏览

    0关注

    423博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

AcWing 840. 模拟散列表(散列hash)

MangataTS 发布时间:2022-01-25 15:48:08 ,浏览量:0

题目连接

https://www.acwing.com/problem/content/description/842/

思路

使用开放寻址法,思路大概是这样我们讲要查询的数模上一个大于n的质数,然后这表示它呗映射到了哪个坑位,如果这个坑位有人的话,那么我们就往下走(假设这些坑位是一个环状),因为我们的坑位数是大于我们要映射的数的,所以不会死循环,这个算法的期望复杂度是O(1)的,但是随着我们映射关系的增加这个常数会变得很大的

代码
#include
using namespace std;
//----------------自定义部分----------------
#define ll long long
#define mod 1000000007
#define endl "\n"
#define PII pair

int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};

ll ksm(ll a,ll b) {
	ll ans = 1;
	for(;b;b>>=1LL) {
		if(b & 1) ans = ans * a % mod;
		a = a * a % mod;
	}
	return ans;
}

ll lowbit(ll x){return -x & x;}

const int N = 200003;
//----------------自定义部分----------------
int n,m,q,a[N],null=0x3f3f3f3f;

int find(int x){
	int t = (x%N+N)%N;
	while(a[t] != null && a[t] != x){
		t=(t+1)%N;
	}
	return t;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);
	cin>>n;
	memset(a,0x3f,sizeof a);
	string op;
	int x;
	for(int i = 1;i >op>>x;
		int t = find(x);
		if(op == "I") a[t] = x;
		else{
			if(a[t] == x) cout            
关注
打赏
1665836431
查看更多评论
0.0380s