Advertisement
Guest User

rodende

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