SashkoKlincharov

[Java][АПС] - Датуми

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