PAT_A1145#Hashing - Average Search Time
Source:
Description:
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be ( where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 1. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 1.
Output Specification:
For each test case, in case it is impossible to insert some number, print in a line
X cannot be inserted.
whereX
is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.
Sample Input:
4 5 4
10 6 4 15 11
11 4 15 2
Sample Output:
15 cannot be inserted.
2.8
Keys:
- 散列(Hash)
Attention:
- 二次探测法K的范围,0<= k <= Size
Code:
- /*
- Data: 2019-08-05 20:14:25
- Problem: PAT_A1145#Hashing - Average Search Time
- AC: 32:04
- 题目大意:
- 哈希表中插入一些列正整数,再查找一系列正整数并计算平均查找时间;
- 表长为不小于给定表长的最小素数,冲突处理采用二次探测法(只取正K)
- 输入:
- 第一行给出,表长Size,待插入总数N,待查找总数M,均<=1e4;
- 第二行给出,N个待插入元素<=1e5
- 第三行给出,M个待查找元素<=1e5
- 输出:
- 若N个数中,有无法插入哈希表的,输出之;
- 计算M个数的平均查找时间,保留一位小数;
- */
- #include<cstdio>
- const int M=1e5+;
- int ht[M]={},mp[M]={};
- bool IsPrime(int x)
- {
- if(x== || x==)
- return false;
- for(int i=; i*i<=x; i++)
- if(x%i==)
- return false;
- return true;
- }
- int main()
- {
- #ifdef ONLINE_JUDGE
- #else
- freopen("Test.txt", "r", stdin);
- #endif // ONLINE_JUDGE
- int T,n,m,x,sum=;
- scanf("%d%d%d", &T,&n,&m);
- while(!IsPrime(T))
- T++;
- for(int i=; i<n; i++)
- {
- scanf("%d", &x);
- for(int j=; j<=T; j++)
- {
- if(ht[(x+j*j)%T]==)
- {
- ht[(x+j*j)%T]=x;
- mp[x]=j+;
- break;
- }
- }
- if(mp[x]==)
- {
- printf("%d cannot be inserted.\n",x);
- mp[x]=T+;
- }
- }
- for(int i=; i<m; i++)
- {
- scanf("%d", &x);
- if(mp[x]==)
- {
- for(int j=; j<=T; j++){
- if(ht[(x+j*j)%T]==){
- sum += (j+);
- break;
- }
- }
- }
- else
- sum += mp[x];
- }
- printf("%.1f", 1.0*sum/m);
- return ;
- }
PAT_A1145#Hashing - Average Search Time的更多相关文章
- PAT 1145 Hashing - Average Search Time [hash][难]
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...
- [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)
1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突
Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...
- 1145. Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...
- PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- PAT 1145 Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 1145. Hashing - Average Search Time (25)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
随机推荐
- NTP-网络时间协议
前言 搭建了一个linux虚拟机,发现跑着跑着时间有差异,查资料发现这个是正常的,因为系统时间和硬件时间是本地时间,会因各种原因导致跟标准时间UTC时间有差异,所以计算机界的牛人发明了NTP协议,专门 ...
- dba 和 rdba 转载
一. DB(Data block) A data block is the smallest unit of storage in an Oracle database. Every datab ...
- 027依据前序遍历和中序遍历,重建二叉树(keep it up)
剑指offer中题目:http://ac.jobdu.com/problem.php?pid=1385 题目描写叙述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.如果输入的前序遍历和中 ...
- HDOJ题目3440 House Man(差分约束)
House Man Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- rsync + inotify 打造多server间文件实时同步
在上篇文章ssh无password登陆server的基础之上.能够利用rsync + Inotify 在多server间实现文件自己主动同步. 例如以下測试机基于三台server做的.内网IP分别例如 ...
- HDU1573 X问题【一元线性同余方程组】
题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1573 题目大意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X ...
- splay树入门(带3个例题)
splay树入门(带3个例题) 首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. PS:若代码有误,请尽快与本人联系,我会尽快改正 首先引入一下splay的概念,他的中文名 ...
- 交叉熵代价函数——当我们用sigmoid函数作为神经元的激活函数时,最好使用交叉熵代价函数来替代方差代价函数,以避免训练过程太慢
交叉熵代价函数 machine learning算法中用得很多的交叉熵代价函数. 1.从方差代价函数说起 代价函数经常用方差代价函数(即采用均方误差MSE),比如对于一个神经元(单输入单输出,sigm ...
- python lmdb demo 这接口和BDB一样恶心啊!
import lmdb lmdb_img_name = "test.lmdb" env = lmdb.open(lmdb_img_name, map_size=1e6) with ...
- codeforces 916E Jamie and Tree dfs序列化+线段树+LCA
E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...