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

哈希表题目集

哈希表都是用来快速判断一个元素是否出现集合里。

#include <stdio.h> #include <stdlib.h> #include <string.h> #define HASH_SIZE 2000003 int hashtable[HASH_SIZE]; int occupied[HASH_SIZE]; int hash(int x) { return (x%HASH_SIZE+HASH_SIZE)%HASH_SIZE; } void insert(int x) { int idx=hash(x); while(occupied[idx]) { if(hashtable[idx]==x) { return; } idx=(idx+1)%HASH_SIZE; } hashtable[idx]=x; occupied[idx]=1; } int query(int x) { int idx=hash(x); while(occupied[idx]) { if(hashtable[idx]==x) return 1; idx=(idx+1)%HASH_SIZE; } return 0; } int main(int argc, char *argv[]) { int q; scanf("%d",&q); memset(occupied,0,sizeof(occupied)); while(q--) { char op[2]; int x; scanf("%s %d",op,&x); if(op[0]=='I') insert(x); else if(op[0]=='Q') { if(query(x)) printf("Yes\n"); else printf("No\n"); } } return 0; }

#include <stdio.h> #include <stdlib.h> #include <string.h> #define HASH_SIZE 20011 typedef struct Node { char* key; struct Node* next; } Node; Node* hashTable[HASH_SIZE]; // 哈希函数 unsigned int hash(const char* str) { unsigned int h = 0; while (*str) { h = h * 131 + (*str++); } return h % HASH_SIZE; } // 插入并返回是否是新元素 int insert(const char* str) { unsigned int index = hash(str); Node* p = hashTable[index]; // 查找是否已存在 while (p) { if (strcmp(p->key, str) == 0) { return 0; } p = p->next; } // 插入新节点 Node* new_node = (Node*)malloc(sizeof(Node)); if (!new_node) return 0; new_node->key = (char*)malloc(strlen(str) + 1); if (!new_node->key) { free(new_node); return 0; } strcpy(new_node->key, str); new_node->next = hashTable[index]; hashTable[index] = new_node; return 1; } int main() { int n; scanf("%d", &n); // 初始化哈希表 for (int i = 0; i < HASH_SIZE; i++) { hashTable[i] = NULL; } int count = 0; // 分配缓冲区 char* buffer = (char*)malloc(2000001); if (!buffer) { printf("0\n"); return 1; } for (int i = 0; i < n; i++) { scanf("%s", buffer); count += insert(buffer); } printf("%d\n", count); // 释放内存 for (int i = 0; i < HASH_SIZE; i++) { Node* p = hashTable[i]; while (p) { Node* temp = p; p = p->next; free(temp->key); free(temp); } } free(buffer); return 0; }
http://www.cnnetsun.cn/news/1557934.html

相关文章:

  • 24C系列EEPROM驱动库:跨页写入与I²C时序可靠性实现
  • StructBERT文本相似度计算:WebUI零基础入门,快速上手教程
  • 手把手教你用vLLM部署GLM-4-9B-Chat-1M,Chainlit前端让对话更直观
  • 入行网络安全,普通人最佳逆袭机会!
  • 树莓派Pico玩转OV7670:低成本图像采集方案从入门到精通
  • Godot 4 Open RPG完整指南:快速构建回合制角色扮演游戏 [特殊字符]
  • 如何构建低延迟Live2D交互系统?从协议到落地的完整实时交互架构方案
  • 技术革命:Legacy-iOS-Kit如何颠覆传统iOS设备维护范式
  • MidScene:零代码AI自动化工具终极指南
  • PyCharm缓存优化指南:避免系统盘被占满的5个实用技巧
  • Claude HUD:AI开发效率的实时状态监控工具
  • F3D:为什么这款极简3D查看器能让你彻底告别传统软件的臃肿?
  • 3个步骤掌握Book Searcher:从安装到实战高效图书检索工具
  • 别再手动重启了!用Docker Compose 5分钟搞定xxl-job高可用集群(附Nginx配置)
  • 3大维度重构企业文档流程:开源ERP系统自动化解决方案
  • 24小时运行:OpenClaw定时调用Qwen3.5-4B-Claude监控竞品动态
  • 避坑指南:在Ubuntu 20.04 + CUDA 11.8环境下,从零搭建SAM2训练环境(含PyTorch 2.5.0版本匹配)
  • 3DS原生GBA游戏体验:open_agb_firm完整使用指南
  • 突破限制:wechat-need-web浏览器插件全攻略
  • 你的电脑为何越用越慢?用Mem Reduct实时内存管理工具让系统重获新生
  • FPGA时序约束进阶:搞懂set_clock_groups里asynchronous和exclusive的区别与应用场景
  • 从模型到应用:深入解析Source-Free Domain Adaptation(SFDA)的核心挑战与实战策略
  • 2026年03月29日全球AI前沿动态
  • 为什么你以为自己在努力工作,产品却没有前进
  • 终极指南:WeKnora实时文档协作与智能检索联动机制详解
  • ConfuserEx终极指南:5分钟掌握.NET程序混淆保护技术
  • YOLO12保姆级教程:从零部署ins-yolo12-independent-v1镜像(含API调用详解)
  • 3步颠覆传统绘图流程的本地创作工具
  • Qwen3-TTS-VoiceDesign应用案例:无障碍阅读工具支持10语种语音朗读
  • 从2D到3D:Meta Quest摄像头数据在Unity中的坐标转换全解析