当前位置: 首页 > news >正文

Hash表

哈希表存储结构(开放寻址法,拉链法)字符串哈希方式(添加、查找h(x))

常见从0~10^9映射到0~10^5就要对10^5取mod(取模一般要质数最好)但是可能会有冲突

1.拉链法:O(1),每个节点拉一条链增加数

#include<iostream>
#include<cstring>
using namespace std;
const int N=100003;
int h[N],e[N],ne[N],idx;
void insert(int x){int k=(x%N+N)%N;e[idx]=x;ne[idx]=h[k];h[k]=idx++;
}
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;
}
int main(){int n;cin>>n;memset(h,-1,sizeof(h));while(n--){char op[2];int x;cin>>op>>x;if(op[0]=='I')insert(x);else {if(find(x))cout<<"Yes"<<endl;else cout<<"No"<<endl;}}return 0;
}

2.开放寻址法:开放寻(厕)法

#include<iostream>
#include<cstring>
using namespace std;
const int N=200003,null=0x3f3f3f3f;
int h[N];
int find(int x ){int k= (x%N+N)%N;while(h[k]!=null&&h[k]!=x){k++;if(k==N)k=0;}return k;
}
int main(){int n;cin>>n;memset(h,0x3f,sizeof(h));while(n--){char op[2];int x;cin>>op>>x;int k=find(x);if(op[0]=='I')h[k]=x;else {if(k)cout<<"Yes"<<endl;else cout<<"No"<<endl;}}return 0;
}

字符串前缀哈希方式:映射成从1开始取p=131   13331  2^64三个

#include<iostream>
using namespace std;
typedef unsigned long long ull;
const int N=100010,P=131;//进制 
int n,m;
char str[N];
ull h[N],p[N];//h[i]表示前i个数的哈希值,p用于存储p的多少次方 
ull get(int l,int r){return h[r]-h[l-1]*p[r-l+1];
} 
int main(){cin>>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;cin>>l1>>r1>>l2>>r2;if(get(l1,r1)==get(l2,r2))cout<<"Yes"<<endl;else cout<<"No"<<endl;}return 0;
}

http://www.lryc.cn/news/527439.html

相关文章:

  • 题解:P10972 I-Country
  • linux常用加固方式
  • 笔灵ai写作技术浅析(二):自然语言处理
  • PyCharm介绍
  • 深度解析:基于Vue 3与Element Plus的学校管理系统技术实现
  • Python从0到100(八十五):神经网络-使用迁移学习完成猫狗分类
  • 苍穹外卖 项目记录 day09 历史订单
  • 记录 | 基于Docker Desktop的MaxKB安装
  • WordPress web-directory-free插件存在本地文件包含导致任意文件读取漏洞(CVE-2024-3673)
  • LLM:BERT or BART 之BERT
  • EtherCAT主站IGH-- 18 -- IGH之fsm_mbox_gateway.h/c文件解析
  • 深入探讨防抖函数中的 this 上下文
  • 【AI论文】魔鬼在细节:关于在训练专用混合专家模型时实现负载均衡损失
  • Gurobi基础语法之addVar 和 addVars
  • C语言学习阶段性总结(五)---函数
  • K8S 快速实战
  • java后端之事务管理
  • 【Redis】缓存+分布式锁
  • 二分查找题目:寻找两个正序数组的中位数
  • 网络安全 | F5-Attack Signatures详解
  • Redis --- 分布式锁的使用
  • LeetCode100之全排列(46)--Java
  • goframe 博客分类文章模型文档 主要解决关联
  • 【JavaWeb06】Tomcat基础入门:架构理解与基本配置指南
  • 安卓日常问题杂谈(一)
  • Kitchen Racks 2
  • 嵌入式学习笔记-杂七杂八
  • 14-7C++STL的stack容器
  • Vue 3 中的响应式系统:ref 与 reactive 的对比与应用
  • 物业巡更系统助推社区管理智能化与服务模式创新的研究与应用