sicily 1003. hash
2024-10-01 16:01:55
Description
请用HASH链式法来解决冲突,且规定链表在链表头插入新元素。
规定HASH函数为:h(x) = x % 11,即哈希数组下标为0~10.
给定两种操作:
- I 操作,插入一个新的正整数K到哈希表中
- F 操作,查询整数K是否在哈希表中,若不在,输出-1;若存在,输出目前K在所在链表中的顺序索引(假定链表头索引为0)。
Input
一组数据。
第一行为整数N,0 < N <= 10000。
接下来N行为随机出现的I操作或F操作。(数据保证K不会重复,K为大于0的int类型)
Output
对于每行F操作,按顺序输出结果。
Sample Input
6
F 10
I 10
I 21
F 10
I 32
F 10
Sample Output
-1
1
2
因为要求比较低我就直接用了数组。
我的代码如下:
#include <iostream>
using namespace std; int main() {
int n;
cin>>n;
int hash[][n+];
for (int i = ; i < ; i++) hash[i][] = ; // hash[i][0] store the count
for (int i = ; i < n; i++) {
char operation;
int num;
cin>>operation>>num;
int mod = num % ;
if (operation == 'F') { // find
int pos = -;
for (int j = ; j <= hash[mod][]; j++) {
if (hash[mod][j] != num) continue;
pos = j;
break;
}
if (pos == -) cout<<-<<endl;
else cout<<hash[mod][] - pos<<endl;
} else { // insert
hash[mod][]++;
hash[mod][hash[mod][]] = num;
}
}
return ;
}
最新文章
- HTML 事件(一) 事件的介绍
- [慢查优化]建索引时注意字段选择性 &; 范围查询注意组合索引的字段顺序
- About_Web
- centos下在线安装mysql
- vs2012中使用Icenium开发应用
- jQuery常用操作方法及常用函数总结
- datanode启动不了
- jquery的$.ajax async使用详解
- Web Farm和Web Garden的区别
- insertAfter()
- ASP.NET SignalR2持久连接层解析
- core dump + LINUX 内核系列博客
- PHP中截取中文乱码
- java的通信机制
- 修改已经提交到远端的git commit信息
- 使用HTML5 FormData对象实现大文件分块上传(断点上传)功能
- jenkins简单安装及配置(Windows环境)
- Django学习笔记二
- flash 概要分析器
- 【jenkins git】Failed to connect to repository:Error performing command:git.exe ls-remote-h