Advertisement
Chajki111

a

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