Source:

PAT A1145 Hashing - Average Search Time (25 分)

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.where X 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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  4. PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突

    Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...

  5. 1145. Hashing - Average Search Time

      The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...

  6. 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 ...

  7. PAT 甲级 1145 Hashing - Average Search Time

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...

  8. PAT 1145 Hashing - Average Search Time

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  9. 1145. Hashing - Average Search Time (25)

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

随机推荐

  1. [转]十五天精通WCF——第十天 学会用SvcConfigEditor来简化配置

    我们在玩wcf项目的时候,都是自己手工编写system.serviceModel下面的配置,虽然在webconfig中做wcf的服务配置的时候,vs提供大多 数的代码提示,但对于不太熟悉服务配置的小鸟 ...

  2. js+jquery动态设置/添加/删除/获取元素属性的两种方法集锦对照(动态onclick属性设置+动态title设置)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html140 ...

  3. A*(也叫A star, A星)寻路算法Java版

    寻路算法有非常多种,A*寻路算法被公觉得最好的寻路算法. 首先要理解什么是A*寻路算法,能够參考这三篇文章: http://www.gamedev.net/page/resources/_/techn ...

  4. cocos2dx编译安卓版本号查看C++错误

    首先,在Mac以下相关软件路径,打开"终端",然后输入  pico .bash_profile  回车 export COCOS2DX_ROOT=/Users/bpmacmini0 ...

  5. 从头认识java-15.7 Map(3)-介绍HashMap的工作原理-get方法

    接着上一章节.我们来讨论一下get方法. 1.还是利用上一章节的图 下图引用自:http://www.admin10000.com/document/3322.html 我们简单说一下步骤.就是通过h ...

  6. 2017 Multi-University Training Contest - Team 2 &&hdu 6050 Funny Function

    Funny Function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. iOS开发之KVC全解

    一  KVC的基本概念 1.KVC是Key Value Coding的缩写,意思是键值编码. 在iOS中,提供了一种方法通过使用属性的名称(也就是Key)来间接访问对象属性的方法,这个方法可以不通过g ...

  8. GROUPPING和ROLLUP的基本知识

    1.GROUPPING 是一个聚合函数,它产生一个附加的列,当用 CUBE 或 ROLLUP 运算符添加行时,附加的列输出值为1,当所添加的行不是由 CUBE 或 ROLLUP 产生时,附加列值为0. ...

  9. PCB Genesis增加轮廓字 实现原理

    在Genesis增加汉字自带是不支持增加汉字的,如果需增加汉字需用到CAD 汉字库才可增加汉字,这里介绍一种脱离汉字库实现 Genesis增加轮廓字(如要变为实体,填空Surface可变为实体字) 一 ...

  10. A simple problem(并查集判环)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497 题意:给定一些点和边的关系,判断S点是否 ...