Advertisement
Alcohol102

Untitled

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