Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class HashNode<T>{
- T object;
- String key;
- HashNode(String s,T o){
- this.object = o;
- this.key = s;
- }
- }
- class HashTable{
- HashNode[] Table;
- int size;
- HashTable(int initial){
- this.size = initial;
- this.Table = new HashNode[initial];
- }
- HashTable(){
- this(9151);
- }
- public int hashCode(String S){
- int hash = 7;
- for (int i = 0; i < S.length(); i++) {
- hash = hash*31 + S.charAt(i);
- }
- return hash;
- }
- public int hashKey(String S){
- return (((this.hashCode(S) & (0x7fffffff)) + 3) % this.size);
- }
- public int hashKey(int I){
- return ((I + 3) % this.size);
- }
- void add(HashNode node){
- int k = hashKey(node.key);
- int i = 0;
- if(Table[k] == null){
- Table[k] = node;
- System.out.println("OK");
- }else{
- while(Table[k] != null){
- i++;
- k = hashKey("" + node.key + String.valueOf(i));
- }
- node.object = (String) ("" + node.key + String.valueOf(i));
- node.key = (String) ("" + node.key + String.valueOf(i));
- Table[k] = node;
- System.out.println(node.object);
- }
- }
- }
- public class TestClass {
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- while(sc.hasNext()){
- int tc = sc.nextInt();
- HashTable T = new HashTable(21474837);
- for(int i = 0; i < tc; i++){
- String st = sc.next();
- HashNode N = new HashNode(st,st);
- T.add(N);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement