Hashing - Hard Version (java)

7-18 Hashing - Hard Version(30 分)

Given a hash table of size N, we can define a hash function . 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

思路:

1、由于有明显的先后顺序,就建立一个结构体,每个结构体里包含这个点的数值,和一个LingkedList。有什么元素必须在这个元素之前,就把他加入到LinkedList中。比较大小的时候,就用contains判断两个元素的大小。

2、其实这个题本意上是让用拓扑排序来做,因为有明显的前后指向的顺序。

3、想不通为什么有两个测试点过不去。


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		StreamTokenizer in  = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		in.nextToken();
		int n = (int) in.nval;
		Lnode node [] = new Lnode[n];
		
		for(int i=0;i<n;i++) {
			in.nextToken();
			int num = (int) in.nval;
			node[i] = new Lnode(num);
			node[i].key = i;
		}
		
		
		for(int i=0;i<n;i++) {
			if(node[i].data%n!=i&&node[i].data!=-1) {
				for(int j =node[i].data%n;;j++) {
					if(j>n-1)
						j=j-n;
					if(j==i)
						break;
					node[i].list.add(node[j].key);

				}
				
			}			
		}
		
		int j=0;
		for(int i =1;i<n;i++) {
			Lnode temp = node[i];
			for(j =i-1;j>=0&&node[j].compareto(temp)>0;j--)
				node[j+1] = node[j];
			node[j+1] = temp;
		}
		
		int flag=0;
		for(int i =0;i<n;i++) {
			if(node[i].data!=-1) {
				if(flag!=0)
					System.out.print(" ");
				System.out.print(node[i].data);
				flag=1;
			}
		}
		
		
	}

}
class Lnode{
	int data =0;
	int key =0;
	
	LinkedList<Integer> list = new LinkedList<>();
	
	
	
	public Lnode(int num) {
		// TODO Auto-generated constructor stub
		this.data = num;
	}



	public int compareto(Lnode temp) {
		// TODO Auto-generated method stub
		if(this.data ==-1&&temp.data!=-1)
			return 1;
		if(this.data!=-1&&temp.data==-1)
			return -1;
		if(this.data == -1&&temp.data ==-1)
			return 0;
		
		if(this.list.contains(temp.key))
			return 1;
		else if(temp.list.contains(this.key))
			return -1;
		else {
			return (this.data - temp.data);
		}
	
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值