SashkoKlincharov

[Java][АПС] - Општини температури

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