[DS] Hashing -- Shortest Ver ?

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:
11
33 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 1050


int 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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值