愿明天的题难一点 把我难倒 然后寒假我就可以刷三遍甲级了 这样榜上就会有权顺荣一刷 权顺荣二刷 权顺荣三刷 还有权顺荣
真是棒棒哒!!!深圳地铁prince,你听到了吗???(PS:犯什么呆萌?还不滚去刷题?)
1145 Hashing - Average Search Time(25 分)
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 H(key)=key%TSize 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 10?4??. 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 10?5??.
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
/****************************** author:yomi date:18.9.7 ps: 最让我伤心的当属这题 考试之前特意复习了一遍平方探测法 结果虽然过了样例 但是一分都没得 多亏了这题 70分结束战斗 、不然考95多不好意思啊 我猜想一定是我理解错了题意 T^T 给我四分我也不至于如此伤心啊 哇哇哇哇 通过运行柳神的Ac代码 分析结果 我就发现了 我逻辑错误啊 基本的哈希没问题吧是求平均查找时间那里出了问题 我完全跳进了自己给自己挖的坑 该 真该 这个平均查找时间 就是长查找时间 而我则贪图方便 在前面进行哈希的时候就计算好了 但是 首先我搞错插不进表的数据的比较次数 自然也搞错了表中没有 的数据的查找次数 其他表中能找到的数据的比较次数是没有问题的。所以只需做一点点调整 但是错了就是错了 25分就是一分没得。 想想70分都已经超过我的水平了吧 我觉得我是40分的水平 明天2200+考生在必定是一场恶战 我估计我能考20分 [滑稽脸] ******************************/ /** 柳婼的代码 ****/ #include <iostream> #include <vector> using namespace std; bool isprime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int main() { int tsize, n, m, a; scanf("%d %d %d", &tsize, &n, &m); while(!isprime(tsize)) tsize++; vector<int> v(tsize); for (int i = 0; i < n; i++) { scanf("%d", &a); int flag = 0; for (int j = 0; j < tsize; j++) { int pos = (a + j * j) % tsize; if (v[pos] == 0) { v[pos] = a; flag = 1; break; } } if (!flag) printf("%d cannot be inserted.\n", a); } int ans = 0; for (int i = 0; i < m; i++) { scanf("%d", &a); for (int j = 0; j <= tsize; j++) { ans++; int pos = (a + j * j) % tsize; if (v[pos] == a || v[pos] == 0) break; } } printf("%.lf\n", ans * 1.0 / m); return 0; } /** 按照正确思路改了不到五行 22分 弃了弃了 去看简洁的代码喽 ***/ //#include<iostream> //#include <cmath> //#include <cstdio> //#include <cstring> //using namespace std; //const int maxn = 100010; //bool isPrime(int n) //{ // if(n == 1){ // return false; // } // if(n == 2){ // return true; // } // for(int i=2; i<=sqrt(n); i++){ // if(n%i == 0){ // return false; // } // } // return true; //} //int hash_table[maxn], tim[maxn]; //int Msize; //int Hash(int n) //{ // int t = 0; // if(hash_table[n%Msize] == 0){ // hash_table[n%Msize] = n; // t++; // } // else{ // int step = 1, flag = 0; // for(int i=0; i<Msize*2; i++){ // int pos = (n+step*step)%Msize; // if(hash_table[pos] == 0){ // hash_table[pos] = n; // flag = 1; // break; // } // step++; // } // if(flag == 0){ // t = Msize+1; // cout << n << " cannot be inserted." << endl; // } // else{ // t = step+1; // } // } // // return t; //} //int main() //{ // int n, m; // memset(tim, 0, sizeof(tim)); // memset(hash_table, 0, sizeof(hash_table)); // cin >> Msize >> n >> m; // if(!isPrime(Msize)){ // for(int i=Msize+1; i<=maxn; i++){ // if(isPrime(i)){ // Msize = i; // break; // } // } // } // int d, q; // for(int i=0; i<n; i++){ // cin >> d; // tim[d] = Hash(d); // } // double ans = 0; // for(int i=0; i<m; i++){ // cin >> q; // if(tim[q]!=0) // ans += tim[q]*1.0; // else{ // tim[q] = Hash(q); // ans += tim[q]; // // } // //cout << ans << endl; // } // printf("%.1f\n", ans/m); // return 0; //} /** Sample Input: 4 5 4 10 6 4 15 11 11 4 15 2 Sample Output: 15 cannot be inserted. 2.8 **/
原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/9606495.html