SashkoKlincharov

[Java][АПС] - Познати имиња

Feb 6th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class CBHT<K extends Comparable<K>, E> {
  4.  
  5. // An object of class CBHT is a closed-bucket hash table, containing
  6. // entries of class MapEntry.
  7. private SLLNode<MapEntry<K,E>>[] buckets;
  8.  
  9. @SuppressWarnings("unchecked")
  10. public CBHT(int m) {
  11. // Construct an empty CBHT with m buckets.
  12. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  13. }
  14.  
  15. private int hash(K key) {
  16. // Translate key to an index of the array buckets.
  17. return Math.abs(key.hashCode()) % buckets.length;
  18. }
  19.  
  20. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  21. // Find which if any node of this CBHT contains an entry whose key is
  22. // equal
  23. // to targetKey. Return a link to that node (or null if there is none).
  24. int b = hash(targetKey);
  25. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  26. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  27. return curr;
  28. }
  29. return null;
  30. }
  31.  
  32. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  33. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  34. int b = hash(key);
  35. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  36. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  37. // Make newEntry replace the existing entry ...
  38. curr.element = newEntry;
  39. return;
  40. }
  41. }
  42. // Insert newEntry at the front of the 1WLL in bucket b ...
  43. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  44. }
  45.  
  46. public void delete(K key) {
  47. // Delete the entry (if any) whose key is equal to key from this CBHT.
  48. int b = hash(key);
  49. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  50. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  51. if (pred == null)
  52. buckets[b] = curr.succ;
  53. else
  54. pred.succ = curr.succ;
  55. return;
  56. }
  57. }
  58. }
  59.  
  60. public String toString() {
  61. String temp = "";
  62. for (int i = 0; i < buckets.length; i++) {
  63. temp += i + ":";
  64. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  65. temp += curr.element.toString() + " ";
  66. }
  67. temp += "\n";
  68. }
  69. return temp;
  70. }
  71.  
  72. }
  73. class SLLNode<E> {
  74. protected E element;
  75. protected SLLNode<E> succ;
  76.  
  77. public SLLNode(E elem, SLLNode<E> succ) {
  78. this.element = elem;
  79. this.succ = succ;
  80. }
  81.  
  82. @Override
  83. public String toString() {
  84. return element.toString();
  85. }
  86. }
  87. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  88.  
  89. // Each MapEntry object is a pair consisting of a key (a Comparable
  90. // object) and a value (an arbitrary object).
  91. K key;
  92. E value;
  93.  
  94. public MapEntry (K key, E val) {
  95. this.key = key;
  96. this.value = val;
  97. }
  98.  
  99. public int compareTo (K that) {
  100. // Compare this map entry to that map entry.
  101. @SuppressWarnings("unchecked")
  102. MapEntry<K,E> other = (MapEntry<K,E>) that;
  103. return this.key.compareTo(other.key);
  104. }
  105.  
  106. public String toString () {
  107. return "<" + key + "," + value + ">";
  108. }
  109. }
  110.  
  111. public class PoznatiIminja {
  112.  
  113. public static void main(String[] args) {
  114. Scanner input = new Scanner(System.in);
  115. int n = input.nextInt();
  116. int m = input.nextInt();
  117. input.nextLine();
  118.  
  119. CBHT<String,Integer> hashtable = new CBHT<String,Integer>(n*2);
  120. for(int i=0;i<n;i++) {
  121. String [] zensko = input.nextLine().split(" ");
  122. String imeZensko = zensko[0];
  123. int br = Integer.parseInt(zensko[1]);
  124. hashtable.insert(imeZensko, br);
  125. }
  126. while(m!=0) {
  127. String [] masko = input.nextLine().split(" ");
  128. String imeMasko = masko[0];
  129. int br = Integer.parseInt(masko[1]);
  130.  
  131. SLLNode<MapEntry<String,Integer>> node = hashtable.search(imeMasko);
  132. if(node!=null) {
  133. System.out.println(node.element.key + " " + node.element.value + " " + br);
  134. }
  135.  
  136. --m;
  137. }
  138.  
  139. }
  140.  
  141. }
Add Comment
Please, Sign In to add comment