Advertisement
Guest User

Apteka

a guest
Mar 21st, 2019
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.44 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  6.  
  7.     // Each MapEntry object is a pair consisting of a key (a Comparable
  8.     // object) and a value (an arbitrary object).
  9.     K key;
  10.     E value;
  11.  
  12.     public MapEntry(K key, E val) {
  13.         this.key = key;
  14.         this.value = val;
  15.     }
  16.  
  17.     public int compareTo(K that) {
  18.         // Compare this map entry to that map entry.
  19.         @SuppressWarnings("unchecked")
  20.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  21.         return this.key.compareTo(other.key);
  22.     }
  23.  
  24.     public String toString() {
  25.         return "<" + key + "," + value + ">";
  26.     }
  27. }
  28.  
  29. class CBHT<K extends Comparable<K>, E> {
  30.  
  31.     // An object of class CBHT is a closed-bucket hash table, containing
  32.     // entries of class MapEntry.
  33.     private SLLNode<MapEntry<K, E>>[] buckets;
  34.  
  35.     @SuppressWarnings("unchecked")
  36.     public CBHT(int m) {
  37.         // Construct an empty CBHT with m buckets.
  38.         buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
  39.     }
  40.  
  41.     private int hash(K key) {
  42.         // Translate key to an index of the array buckets.
  43.         return Math.abs(key.hashCode()) % buckets.length;
  44.     }
  45.  
  46.     public SLLNode<MapEntry<K, E>> search(K targetKey) {
  47.         // Find which if any node of this CBHT contains an entry whose key is
  48.         // equal
  49.         // to targetKey. Return a link to that node (or null if there is none).
  50.         int b = hash(targetKey);
  51.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  52.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  53.                 return curr;
  54.         }
  55.         return null;
  56.     }
  57.  
  58.     public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  59.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  60.         int b = hash(key);
  61.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  62.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  63.                 // Make newEntry replace the existing entry ...
  64.                 curr.element = newEntry;
  65.                 return;
  66.             }
  67.         }
  68.         // Insert newEntry at the front of the 1WLL in bucket b ...
  69.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  70.     }
  71.  
  72.     public void delete(K key) {
  73.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  74.         int b = hash(key);
  75.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  76.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  77.                 if (pred == null)
  78.                     buckets[b] = curr.succ;
  79.                 else
  80.                     pred.succ = curr.succ;
  81.                 return;
  82.             }
  83.         }
  84.     }
  85.  
  86.     public String toString() {
  87.         String temp = "";
  88.         for (int i = 0; i < buckets.length; i++) {
  89.             temp += i + ":";
  90.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  91.                 temp += curr.element.toString() + " ";
  92.             }
  93.             temp += "\n";
  94.         }
  95.         return temp;
  96.     }
  97.  
  98. }
  99.  
  100. class SLLNode<E> {
  101.     protected E element;
  102.     protected SLLNode<E> succ;
  103.  
  104.     public SLLNode(E elem, SLLNode<E> succ) {
  105.         this.element = elem;
  106.         this.succ = succ;
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         return element.toString();
  112.     }
  113. }
  114.  
  115. class ImeLek implements Comparable<ImeLek> {
  116.     String imeLek;
  117.     // najprvin implementirame konstruktor, pa hashCode pa equals. hashCodot treba
  118.     // sami da go izmenime
  119.  
  120.     public ImeLek(String imeLek) {
  121.         super();
  122.         this.imeLek = imeLek;
  123.     }
  124.  
  125.     @Override
  126.     public int hashCode() {
  127.  
  128.         /* h(w)=(29∗(29∗(29∗0+ASCII(c1))+ASCII(c2))+ASCII(c3))%102780 */
  129.         int result;
  130.         result = (29 * (29 * (29 * 0 + imeLek.charAt(0)) + imeLek.charAt(1)) + imeLek.charAt(2)) % 102780;
  131.         return result;
  132.     }
  133.  
  134.     @Override
  135.     public boolean equals(Object obj) {
  136.         if (this == obj)
  137.             return true;
  138.         if (obj == null)
  139.             return false;
  140.         if (getClass() != obj.getClass())
  141.             return false;
  142.         ImeLek other = (ImeLek) obj;
  143.         if (imeLek == null) {
  144.             if (other.imeLek != null)
  145.                 return false;
  146.         } else if (!imeLek.equals(other.imeLek))
  147.             return false;
  148.         return true;
  149.     }
  150.  
  151.     @Override
  152.     public int compareTo(ImeLek arg0) {
  153.         // TODO Auto-generated method stub
  154.         return imeLek.compareTo(arg0.imeLek);
  155.     }
  156.  
  157. }
  158. class Lek{
  159.     String imeLek;
  160.     int pozneg;
  161.     int cena;
  162.     int kolichina;
  163.  
  164.     // najprvin implementirame konstruktor, pa hashCode pa equals. hashCodot treba
  165.     // sami da go izmenime
  166.  
  167.     public Lek(String imeLek, int pozneg, int cena, int kolichina) {
  168.         super();
  169.         this.imeLek = imeLek;
  170.         this.pozneg = pozneg;
  171.         this.cena = cena;
  172.         this.kolichina = kolichina;
  173.     }
  174.  
  175.     public String toString() {
  176.         String s = new String();
  177.         s += imeLek + "\n";
  178.         if (pozneg == 1) { // s+=(pozneg==1?"poz\n":"neg\n");
  179.             s += "poz\n";
  180.  
  181.         } else
  182.             s += "neg\n";
  183.         s += cena + "\n";
  184.         s += kolichina;
  185.         return s;
  186.     }
  187. }
  188.  
  189.  
  190.  
  191. public class HashApteka {
  192.  
  193.     public static void main(String[] args) throws IOException {
  194.         // TODO Auto-generated method stub
  195.  
  196.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  197.  
  198.         int N, i, pozneg, cena, kolichina;
  199.         String s, imeLek;
  200.  
  201.         ImeLek il;
  202.         Lek l;
  203.  
  204.         SLLNode<MapEntry<ImeLek, Lek>> p = null;
  205.         String[] pom;
  206.         s = br.readLine();
  207.         N = Integer.parseInt(s);
  208.        
  209.         CBHT<ImeLek, Lek> tabela = new CBHT<ImeLek, Lek>(2 * N + 1);
  210.  
  211.    
  212.         for (i = 0; i < N; i++) {
  213.             s = br.readLine();
  214.             pom = s.split(" ");
  215.             imeLek = pom[0].toUpperCase();
  216.             pozneg = Integer.parseInt(pom[1]);
  217.             cena = Integer.parseInt(pom[2]);
  218.             kolichina = Integer.parseInt(pom[3]);
  219.             il = new ImeLek(imeLek);
  220.             l = new Lek(imeLek, pozneg, cena, kolichina);
  221.             tabela.insert(il, l);
  222.         }
  223.  
  224.         s = br.readLine();
  225.         while (!s.equals("KRAJ")) {
  226.             imeLek = s.toUpperCase();
  227.             s = br.readLine();
  228.             kolichina = Integer.parseInt(s);
  229.             il = new ImeLek(imeLek);
  230.            
  231.             /* il = new ImeLek(imeLek, kolichina, 0, 0);  mozhe i vaka ama kolichina ne ja zema
  232.             bidejki imeLek e keyot*/
  233.             p = tabela.search(il);
  234.             if (p == null) {
  235.                 System.out.println("Nema takov lek");
  236.  
  237.             } else {
  238.                 Lek najden = p.element.value;
  239.                 /*
  240.                  * System.out.println(najden.imeLek); if((najden.pozneg)==1) {
  241.                  * System.out.println("poz");
  242.                  *
  243.                  * } else System.out.println("neg"); System.out.println(najden.cena);
  244.                  * System.out.println(najden.kolichina);
  245.                  */
  246.                 System.out.println(najden);
  247.  
  248.                 if (kolichina <= najden.kolichina) {
  249.                     najden.kolichina -= kolichina;
  250.                     tabela.insert(il, najden);
  251.                     System.out.println("Napravena narachka");
  252.  
  253.                 }
  254.  
  255.                 else
  256.                     System.out.println("Nema dovolno lekovi");
  257.  
  258.             }
  259.  
  260.             s = br.readLine();
  261.         }
  262.  
  263.     }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement