Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lugje sho uchat hash > lugje sho ne uchat hash
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + "->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first==before){
- this.insertFirst(o);
- return;
- }
- //ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first ==node){
- return this.deleteFirst();
- }
- while (tmp.succ != node && tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.element != o && tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public Iterator<E> iterator () {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.element;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- //Not implemented
- }
- }
- public void mirror(){
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while(tmp != null){
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- public void merge (SLL<E> in){
- if (first != null) {
- SLLNode<E> tmp = first;
- while(tmp.succ != null)
- tmp = tmp.succ;
- tmp.succ = in.getFirst();
- }
- else{
- first = in.getFirst();
- }
- }
- }
- class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable
- // object) and a value (an arbitrary object).
- K key;
- E value;
- public MapEntry (K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo (K that) {
- // Compare this map entry to that map entry.
- @SuppressWarnings("unchecked")
- MapEntry<K,E> other = (MapEntry<K,E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString () {
- return "<" + key + "," + value + ">";
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- private SLLNode<MapEntry<K,E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Napishete ja vie HASH FUNKCIJATA
- return Math.abs(hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K,E>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
- return curr;
- }
- return null;
- }
- public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this CBHT.
- int b = hash(key);
- for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(((MapEntry<K,E>) curr.element).key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.element.toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- }
- class Drug{
- public String ime;
- public String bolest;
- public int cena;
- public Drug(String ime, String bolest, int cena) {
- this.ime = ime;
- this.bolest = bolest;
- this.cena = cena;
- }
- }
- public class Apteka {
- public static void main(String[] args) {
- Scanner input= new Scanner(System.in);
- int numDrugs= Integer.parseInt(input.nextLine());
- CBHT<String,SLL<Drug>> drugs= new CBHT<>(499);
- for(int i=0;i<numDrugs;i++){
- String[] info_drug= input.nextLine().split("\\s+");
- if(drugs.search(info_drug[1])==null){
- SLL<Drug> lista = new SLL<Drug>();
- lista.insertLast(new Drug(info_drug[0],info_drug[1],Integer.parseInt(info_drug[2])));
- drugs.insert(info_drug[1],lista);
- }
- else{
- drugs.search(info_drug[1]).element.value.insertLast(new Drug(info_drug[0],info_drug[1],Integer.parseInt(info_drug[2])));
- }
- }
- String sickness= input.nextLine();
- if(drugs.search(sickness)!=null){
- SLLNode<Drug> temp= drugs.search(sickness).element.value.getFirst();
- int lowest_price= temp.element.cena;
- String best_drug= temp.element.ime;
- temp=temp.succ;
- while(temp!=null){
- if(temp.element.cena<lowest_price) {
- best_drug= temp.element.ime;
- lowest_price = temp.element.cena;
- }
- temp=temp.succ;
- }
- System.out.println(best_drug);
- }
- else{
- System.out.println("Samo dobar seks mu e lekot...");
- }
- }
- }
- /*
- 5
- Analgin Glavobolka 80
- Daleron Glavobolka 90
- Lineks Bolki_vo_stomak 150
- Spazmeks Bolki_vo_stomak 150
- Loratadin Alergija 150
- Glavobolka
- --------------------------
- Analgin
- 6
- Analgin Glavobolka 80
- Daleron Glavobolka 70
- Lineks Bolki_vo_stomak 150
- Spazmeks Bolki_vo_stomak 150
- Loratadin Alergija 150
- Paracetamol Glavobolka 100
- Glavobolka
- --------------------------
- Daleron
- 7
- Viagra Erektilna_Disfunkcija 80
- Lineks Bolki_vo_stomak 150
- Spazmeks Bolki_vo_stomak 150
- Loratadin Alergija 150
- Amoxiclav Bolki_vo_grlo 100
- AngiSept Bolki_vo_grlo 69
- Caffetin Zabobolka 75
- Glavobolka
- --------------------------
- Samo dobar seks mu e lekot...
- */
Add Comment
Please, Sign In to add comment