当前位置:网站首页>PAT Class A 1145 Hash - Average Lookup Time
PAT Class A 1145 Hash - Average Lookup Time
2022-08-02 17:03:00 【keyboard sonata】
The task of this question is simple:
First insert an integer sequence consisting of several different positive integers into a hash table, then try to find another integer key value sequence from the table, and output the average search time (the search time refers to finding a valuewhether the number of comparison operations performed in the table).
The hash function is defined as H(key)=key%TSize, where TSize is the maximum size of the hash table.
Use quadratic probing with only positive increments to resolve conflicts.
Note that the size of the hash table is preferably a prime number, if the maximum size given by the user is not a prime number, the table size must be redefined to be the smallest prime number larger than the size given by the user.
Input format
The first line contains three positive integers MSize,N,M, which respectively represent the size of the user-defined table, the number of inserted integers, and the number of lookup key values.The second line contains N distinct positive integers representing the insertion sequence.
The third line contains M positive integers representing key-value sequences.
The numbers in the same row are separated by spaces, and neither sequence contains more than 105 integers.
Output format
If some numbers cannot be inserted, they are output in sequence as follows, one line per number:X cannot be inserted.
Where X represents a number that cannot be inserted.The last line outputs the average search time of M searches, rounded to one decimal place.
Note: If TSize times are searched, and there are numbers in each search position, but not equal to the number to be searched, the search time is considered to be TSize+1.
Data Range
1≤MSize,N,M≤104
Sample input:
4 5 4
10 6 415 11
11 4 15 2
Sample output:
15 cannot be inserted.
2.8
My solution:
#include using namespace std;const int N = 10010;int n, m, s;int h[N];bool is_prime(int n){if(n == 1) return false;for(int i = 2; i * i <= n; i ++ ){if(n % i == 0) return false;}return true;}int find(int x, int &count){int t = x % s;for(int i = 0; i < s; i ++ ){int j = (t + i * i) % s;if(h[j] == 0 || h[j] == x) return j;else count ++ ;}return -1;}int main(){cin >> s >> n >> m;while(!is_prime(s)) s++;for(int i = 0; i < n; i ++ ){int x, count = 1;cin >> x;int t = find(x, count);if(t == -1) printf("%d cannot be inserted.\n", x);else h[t] = x;}int cnt = 0;for(int i = 0; i < m; i ++ ){int x, count = 1;cin >> x;int t = find(x, count);cnt += count;}printf("%.1lf", (double) cnt / m);return 0;} Harvest:
Hash lookup and store value use the same function
边栏推荐
- Reading is the cheapest and noblest
- PAT甲级 1019 普通回文数
- 两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
- Mechanical keyboard failure
- 2021 Huawei Cup Mathematical Modeling Contest E question - Ultra-Wideband (UWB) precise positioning problem under signal interference
- 【JS执行机制】
- 为什么四个字节的float表示的范围比八个字节的long要广
- codeforces Linova and Kingdom
- CSV file with the data read and write 】 【 XLS/XLSX file
- ELK日志分析系统
猜你喜欢
随机推荐
CSV file with the data read and write 】 【 XLS/XLSX file
lammps学习(二)联合原子模型聚乙烯拉伸
codeforces k-Tree (dp仍然不会耶)
2021 Huawei Cup Mathematical Modeling Contest E question - Ultra-Wideband (UWB) precise positioning problem under signal interference
【Leetcode字符串--字符串变换/进制的转换】HJ1.字符串最后一个单词的长度 HJ2.计算某字符出现次数 HJ30.字符串合并处理
JS本地存储(附实例)
Redis的5中数据类型总结
为什么四个字节的float表示的范围比八个字节的long要广
DOM - Event Object
Reading is the cheapest and noblest
李开复花上千万投的缝纫机器人,团队出自大疆
异常简单总结
状态码以及访问百度过程
SOA(面向服务架构)是什么?
公司最大的内卷,是“管理错位”
2022-0801 第六小组 瞒春 学习笔记
2022-02-14 第五小组 瞒春 学习笔记
Window function method for FIR filter design
Principles of permutation entropy, fuzzy entropy, approximate entropy, sample entropy and approximate entropy implemented by MATLAB
PAT甲级 1019 普通回文数









