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 ;
}

最新文章

  1. HTML 事件(一) 事件的介绍
  2. [慢查优化]建索引时注意字段选择性 &amp; 范围查询注意组合索引的字段顺序
  3. About_Web
  4. centos下在线安装mysql
  5. vs2012中使用Icenium开发应用
  6. jQuery常用操作方法及常用函数总结
  7. datanode启动不了
  8. jquery的$.ajax async使用详解
  9. Web Farm和Web Garden的区别
  10. insertAfter()
  11. ASP.NET SignalR2持久连接层解析
  12. core dump + LINUX 内核系列博客
  13. PHP中截取中文乱码
  14. java的通信机制
  15. 修改已经提交到远端的git commit信息
  16. 使用HTML5 FormData对象实现大文件分块上传(断点上传)功能
  17. jenkins简单安装及配置(Windows环境)
  18. Django学习笔记二
  19. flash 概要分析器
  20. 【jenkins git】Failed to connect to repository:Error performing command:git.exe ls-remote-h

热门文章

  1. iOS给label加入下划线
  2. 一个ibatis映射文件的例子(包含增删改单查,多查)
  3. hdu 4628 Pieces(状态压缩+记忆化搜索)
  4. ZOJ 1696 Viva Confetti 计算几何
  5. Elasticsearch yellow 意味着主分片可用,副本不可用
  6. 图论之tarjan缩点
  7. AngularJS1 学习笔记1
  8. GPG加密windows中使用
  9. MyEclipse 启动之 java.lang.RuntimeException: No application id has been
  10. CorelDRAW X6低价再次冲破底线