{"id":1461,"date":"2015-02-25T21:40:05","date_gmt":"2015-02-25T13:40:05","guid":{"rendered":"http:\/\/boweihe.me\/?p=1461"},"modified":"2015-02-25T21:40:05","modified_gmt":"2015-02-25T13:40:05","slug":"1078-hashing-25-%ef%bc%9a%ef%bc%9a%e5%93%88%e5%b8%8c%e8%a1%a8%e4%ba%8c%e6%ac%a1%e6%8e%a2%e6%b5%8b%e6%b3%95%e8%b4%a8%e6%95%b0%e5%88%a4%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1461","title":{"rendered":"1078. Hashing (25) \uff1a\uff1a\u54c8\u5e0c\u8868\u4e8c\u6b21\u63a2\u6d4b\u6cd5&#124;\u8d28\u6570\u5224\u5b9a"},"content":{"rendered":"<p><em><strong>\u539f\u9898<a href=\"http:\/\/www.patest.cn\/contests\/pat-a-practise\/1078\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.patest.cn\/contests\/pat-a-practise\/1078<\/a>\uff1a<\/strong><\/em><br \/>\nThe task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be &#8220;H(key) = key % TSize&#8221; where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.<br \/>\nNote 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.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. For each case, the first line contains two positive numbers: MSize (&lt;=10<sup>4<\/sup>) and N (&lt;=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print &#8220;-&#8221; instead.<br \/>\n<b>Sample Input:<\/b><\/p>\n<pre>4 4\n10 6 4 15\n<\/pre>\n<p><b>Sample Output:<\/b><\/p>\n<pre class=\"\">0 1 4 -<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>&nbsp;<br \/>\n<span style=\"color: #0000ff;\"><strong>\u8fd9\u4e2a\u9898\u76ee\u4e3b\u8981\u662f\u4e24\u4e2a\u95ee\u9898\uff1a<\/strong><\/span><\/p>\n<ol>\n<li>\u8d28\u6570\u7684\u67e5\u627e\u3002\u8d28\u6570\u67e5\u627e\u91c7\u7528\u6bd4\u8f83\u53d6\u5de7\u7684\u7b28\u529e\u6cd5\uff0c1000\u4ee5\u4e0b\u7528\u8d28\u6570\u8868\uff0c1000\u4ee5\u4e0a\u7684\u7528\u571f\u529e\u6cd5\uff08\u9664\u4ee5 2~\u6839\u53f7X\u4e00\u4e2a\u4e2a\u8bd5\uff09\uff1b<\/li>\n<li>\u54c8\u5e0c\u8868\u51b2\u7a81\u7684\u89e3\u51b3\uff0c\u9898\u76ee\u4e2d\u660e\u786e\u5199\u4e86\u4f7f\u7528Quadratic probing(positive increments only)\uff0c\u5373\u5e8f\u53f7\u9012\u589e\u7684\u90a3\u79cd\u4e8c\u6b21\u63a2\u6d4b\u6cd5\u3002\u5177\u4f53\u7ec6\u8282\u5c31\u4e0d\u591a\u8bf4\u4e86\uff0c\u53ef\u4ee5\u53c2\u8003<a href=\"http:\/\/www.360doc.com\/content\/14\/0410\/23\/15257968_367897761.shtml\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fd9\u91cc<\/a>\u3001<a href=\"http:\/\/www.cnblogs.com\/foreverking\/articles\/2339735.html\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fd9\u91cc<\/a>\u548c<a href=\"http:\/\/en.wikipedia.org\/wiki\/Quadratic_probing\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fd9\u91cc<\/a>\u3002\u6570\u636e\u7ed3\u6784\u8352\u5e9f\u591a\u5e74\uff0c\u81ea\u5df1\u7adf\u7136\u8fd8\u8981\u67e5\u8d44\u6599\uff0c\u4e5f\u662f\u633a\u4e0d\u597d\u610f\u601d\u7684\u3002<\/li>\n<\/ol>\n<hr \/>\n<p>&nbsp;<br \/>\n\u6211\u7684\u4ee3\u7801\uff08c++\uff09\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\nusing namespace std;\nint prime_1000[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,\n31, 37, 41, 43, 47, 53, 59, 61, 67, 71,\n73, 79, 83, 89, 97, 101, 103, 107, 109, 113,\n127, 131, 137, 139, 149, 151, 157, 163, 167, 173,\n179, 181, 191, 193, 197, 199, 211, 223, 227, 229,\n233, 239, 241, 251, 257, 263, 269, 271, 277, 281,\n283, 293, 307, 311, 313, 317, 331, 337, 347, 349,\n353, 359, 367, 373, 379, 383, 389, 397, 401, 409,\n419, 421, 431, 433, 439, 443, 449, 457, 461, 463,\n467, 479, 487, 491, 499, 503, 509, 521, 523, 541,\n547, 557, 563, 569, 571, 577, 587, 593, 599, 601,\n607, 613, 617, 619, 631, 641, 643, 647, 653, 659,\n661, 673, 677, 683, 691, 701, 709, 719, 727, 733,\n739, 743, 751, 757, 761, 769, 773, 787, 797, 809,\n811, 821, 823, 827, 829, 839, 853, 857, 859, 863,\n877, 881, 883, 887, 907, 911, 919, 929, 937, 941,\n947, 953, 967, 971, 977, 983, 991, 997, 1009 };\nint findSmallestPrime_(int biggerThan)\n{\n\t\/\/\u6bd41000\u5927\u7684\u5728\u8fd9\u8fb9\u5904\u7406\n\tint currNum = biggerThan;\n\twhile (true)\n\t{\n\t\tbool primeFlag = true;\n\t\tfor (int n = 2; n &lt;= (int)sqrt(currNum); n++)\n\t\t{\n\t\t\tif (currNum%n == 0)\n\t\t\t{\n\t\t\t\tprimeFlag = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (primeFlag)\n\t\t\treturn currNum;\n\t\tcurrNum++;\n\t}\n}\nint findSmallestPrime(int biggerThan)\n{\n\tif (biggerThan &lt; 1000)\n\t{\n\t\t\/\/ &lt;1000\u7684\u76f4\u63a5\u67e5\u8868\n\t\tfor (int i = 0; i &lt; 169; i++)\n\t\t{\n\t\t\tif (prime_1000[i] &lt; biggerThan)\n\t\t\t\tcontinue;\n\t\t\telse\n\t\t\t\treturn prime_1000[i];\n\t\t}\n\t}\n\telse\n\t\tfindSmallestPrime_(biggerThan);\n}\nint getHashPos(int* hashTable, int Tsize, int val)\n{\n\t\/\/\u5982\u679c\u585e\u4e0d\u8fdb\u53bb\u5219\u8fd4\u56de-1\uff0c\u5426\u5219\u8fd4\u56de\u4f4d\u7f6e\n\t\/\/\u4f7f\u7528Quadratic probing\n\tint probIndex = val % Tsize;\n\tint H = probIndex;\n\tint trialCount = 1;\n\twhile (hashTable[probIndex] != -1\n\t\t&amp;&amp; trialCount &lt; Tsize)\n\t{\n\t\tprobIndex = (val + trialCount * trialCount) % Tsize;\n\t\ttrialCount++;\n\t}\n\tif (trialCount &gt;= Tsize)\n\t\treturn -1;\n\thashTable[probIndex] = val;\n\treturn probIndex;\n}\nint main()\n{\n\tint M, N;\n\tcin &gt;&gt; M &gt;&gt; N;\n\tint Tsize = findSmallestPrime(M);\n\tint* hashTable = new int[Tsize];\n\tfor (int i = 0; i &lt; Tsize; i++)\n\t\thashTable[i] = -1;\n\tfor (int i = 0; i &lt; N; i++)\n\t{\n\t\tint currVal;\n\t\tcin &gt;&gt; currVal;\n\t\tint pos = getHashPos(hashTable, Tsize, currVal);\n\t\tif (pos == -1)\n\t\t\tcout &lt;&lt; \"-\";\n\t\telse\n\t\t\tcout &lt;&lt; pos;\n\t\tif (i &lt; N - 1)\n\t\t\tcout &lt;&lt; \" \";\n\t}\n\treturn 0;\n}<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<hr \/>\n<table id=\"case_result_list\">\n<thead>\n<tr>\n<th class=\"header\">\u6d4b\u8bd5\u70b9<\/th>\n<th class=\"header\">\u7ed3\u679c<\/th>\n<th class=\"header\">\u7528\u65f6(ms)<\/th>\n<th class=\"header\">\u5185\u5b58(kB)<\/th>\n<th class=\"header\">\u5f97\u5206\/\u6ee1\u5206<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td><span class=\"caseRes-3\">\u7b54\u6848\u6b63\u786e<\/span><\/td>\n<td>1<\/td>\n<td>360<\/td>\n<td>12\/12<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><span class=\"caseRes-3\">\u7b54\u6848\u6b63\u786e<\/span><\/td>\n<td>1<\/td>\n<td>360<\/td>\n<td>3\/3<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td><span class=\"caseRes-3\">\u7b54\u6848\u6b63\u786e<\/span><\/td>\n<td>1<\/td>\n<td>232<\/td>\n<td>5\/5<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td><span class=\"caseRes-3\">\u7b54\u6848\u6b63\u786e<\/span><\/td>\n<td>20<\/td>\n<td>360<\/td>\n<td>5\/5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u540e\u4e00\u4e2a\u6d4b\u8bd5\u70b9\u5e94\u8be5\u662f\u6bd4\u8f83\u5927\u7684\u6570<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u9898http:\/\/www.patest.cn\/contests\/pat-a-practise\/1078\uff1a The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be &#8220;H(key) = key % TSize&#8221; where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[84],"class_list":["post-1461","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1461"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1461\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}