[DS] Hashing -- Shortest Ver ?

来源:互联网 发布:黄岛java开发招聘信息 编辑:程序博客网 时间:2024/05/17 07:22

Requirement:

 Hashing

时间限制
100 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
HE, Qinming

Given a hash table of size N, we can define a hash function H(x) = x%N. Suppose that the linear probing is used to solve collisions, we can easily obtain the status of the hash table with a given sequence of input numbers.

However, now you are asked to solve the reversed problem: reconstruct the input sequence from the given status of the hash table. Whenever there are multiple choices, the smallest number is always taken.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (<=1000), which is the size of the hash table. The next line contains N integers, separated by a space. A negative integer represents an empty cell in the hash table. It is guaranteed that all the non-negative integers are distinct in the table.

Output Specification:

For each test case, print a line that contains the input sequence, with the numbers separated by a space. Notice that there must be no extra space at the end of each line.

Sample Input:
1133 1 13 12 34 38 27 22 32 -1 21
Sample Output:
1 13 12 21 33 34 38 27 22 32


Analysis:

(Will be updated later


Code:

#include <cstdio>#include <string.h>#include <algorithm>using namespace std;#define MAX_SIZE 1050int used[MAX_SIZE];int ans[MAX_SIZE];struct node {<span style="white-space:pre"></span>int temp;// the original number<span style="white-space:pre"></span>int index;// the index in input array<span style="white-space:pre"></span>int unit;// the unit it belongs to PS:(temp % n)};node b[MAX_SIZE];int cmp(node a,node b){<span style="white-space:pre"></span>return a.temp < b.temp; }/*compared by input number since we are required to output the least avaliable number everytime*/int main(){<span style="white-space:pre"></span>int n;<span style="white-space:pre"></span>scanf("%d",&n);<span style="white-space:pre"></span>for (int i = 0; i < n; i++) {<span style="white-space:pre"></span>int temp;<span style="white-space:pre"></span>scanf("%d",&temp);<span style="white-space:pre"></span>b[i].temp = temp;//cin the data number<span style="white-space:pre"></span>b[i].index = i;//record the index number  <span style="white-space:pre"></span>b[i].unit = temp % n;//calculate the original position which it belongs to<span style="white-space:pre"></span>}<span style="white-space:pre"></span>sort(b,b+n,cmp);//sort by input number (For the least first requirement)<span style="white-space:pre"></span>int start = 0;//the start position<span style="white-space:pre"></span>while (b[start++].temp < 0);//automatically ignore -1 because it never matters<span style="white-space:pre"></span>int cnt, num;//num means the total amount of number (not include -1) cnt is a signal to show whether it is time to stop <span style="white-space:pre"></span>num = cnt = n - start + 1;<span style="white-space:pre"></span>start--;<span style="white-space:pre"></span>int k = start;//turn the pointer to start position<span style="white-space:pre"></span>while(cnt){//still numbers left<span style="white-space:pre"></span>if (used[b[k].index]) {//if it has been output<span style="white-space:pre"></span>k++;//,then go to next<span style="white-space:pre"></span>continue;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else if (b[k].index == b[k].unit) {//if it is at its orginal position<span style="white-space:pre"></span>ans[num - cnt] = b[k].temp;//store the number in ans[]<span style="white-space:pre"></span>used[b[k].index] = 1;//mark it as used<span style="white-space:pre"></span>cnt--;//count minus 1<span style="white-space:pre"></span>k = start;//turn the pointer back to start position<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else {<span style="white-space:pre"></span>int i;<span style="white-space:pre"></span>if (b[k].unit < b[k].index)//check whether all numbers from its original position to its current position have all been output<span style="white-space:pre"></span>for(i = b[k].unit; i < b[k].index && used[i]; i++);<span style="white-space:pre"></span>else {<span style="white-space:pre"></span>for(i = b[k].unit; i < n && used[i]; i++);<span style="white-space:pre"></span>if (i == n)<span style="white-space:pre"></span>for(i = 0; i < b[k].index && used[i]; i++);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (i == b[k].index){//if all numbers from its original position to its current position have all been output<span style="white-space:pre"></span>ans[num - cnt] = b[k].temp;//store the number in ans[]<span style="white-space:pre"></span>used[b[k].index] = 1;//mark it as used<span style="white-space:pre"></span>cnt--;//mark it as used<span style="white-space:pre"></span>k = start;//turn the pointer back to start position<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else k++;/*it this number doesn't meet the requirment, instead of turning the pointer to start position, <span style="white-space:pre"></span>this time we should move it to next position (Otherwise we will keep repeating)*/<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>printf("%d",ans[0]);//output part<span style="white-space:pre"></span>for(int i = 1; i < num; i++)<span style="white-space:pre"></span>printf(" %d",ans[i]);<span style="white-space:pre"></span>printf("\n");<span style="white-space:pre"></span>return 0;}


0 0