SashkoKlincharov

[Java][АПС] - Родендени

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