Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class HashNode<T>{
  4. T object;
  5. String key;
  6.  
  7. HashNode(String s,T o){
  8. this.object = o;
  9. this.key = s;
  10. }
  11. }
  12.  
  13. class HashTable{
  14. HashNode[] Table;
  15. int size;
  16.  
  17. HashTable(int initial){
  18. this.size = initial;
  19. this.Table = new HashNode[initial];
  20. }
  21.  
  22. HashTable(){
  23. this(9151);
  24. }
  25.  
  26. public int hashCode(String S){
  27. int hash = 7;
  28. for (int i = 0; i < S.length(); i++) {
  29. hash = hash*31 + S.charAt(i);
  30. }
  31. return hash;
  32. }
  33.  
  34. public int hashKey(String S){
  35. return (((this.hashCode(S) & (0x7fffffff)) + 3) % this.size);
  36. }
  37.  
  38. public int hashKey(int I){
  39. return ((I + 3) % this.size);
  40. }
  41.  
  42. void add(HashNode node){
  43. int k = hashKey(node.key);
  44. int i = 0;
  45. if(Table[k] == null){
  46. Table[k] = node;
  47. System.out.println("OK");
  48. }else{
  49. while(Table[k] != null){
  50. i++;
  51. k = hashKey("" + node.key + String.valueOf(i));
  52. }
  53. node.object = (String) ("" + node.key + String.valueOf(i));
  54. node.key = (String) ("" + node.key + String.valueOf(i));
  55. Table[k] = node;
  56. System.out.println(node.object);
  57. }
  58. }
  59. }
  60.  
  61. public class TestClass {
  62.  
  63. public static void main(String[] args){
  64. Scanner sc = new Scanner(System.in);
  65. while(sc.hasNext()){
  66. int tc = sc.nextInt();
  67. HashTable T = new HashTable(21474837);
  68. for(int i = 0; i < tc; i++){
  69. String st = sc.next();
  70. HashNode N = new HashNode(st,st);
  71. T.add(N);
  72. }
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement