Advertisement
Josif_tepe

Untitled

Nov 25th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.NoSuchElementException;
  5. import java.util.Scanner;
  6.  
  7. interface Queue<E> {
  8.  
  9.     // Elementi na redicata se objekti od proizvolen tip.
  10.  
  11.     // Metodi za pristap:
  12.  
  13.     public boolean isEmpty ();
  14.     // Vrakja true ako i samo ako redicata e prazena.
  15.  
  16.     public int size ();
  17.     // Ja vrakja dolzinata na redicata.
  18.  
  19.     public E peek ();
  20.     // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  21.  
  22.     // Metodi za transformacija:
  23.  
  24.     public void clear ();
  25.     // Ja prazni redicata.
  26.  
  27.     public void enqueue (E x);
  28.     // Go dodava x na kraj od redicata.
  29.  
  30.     public E dequeue ();
  31.     // Go otstranuva i vrakja pochetniot element na redicata.
  32.  
  33. }
  34. class ArrayQueue<E> implements Queue<E> {
  35.  
  36.     // Redicata e pretstavena na sledniot nacin:
  37.     // length go sodrzi brojot na elementi.
  38.     // Ako length > 0, togash elementite na redicata se zachuvani vo elems[front...rear-1]
  39.     // Ako rear > front, togash vo  elems[front...maxlength-1] i elems[0...rear-1]
  40.     E[] elems;
  41.     int length, front, rear;
  42.  
  43.     // Konstruktor ...
  44.  
  45.     @SuppressWarnings("unchecked")
  46.     public ArrayQueue (int maxlength) {
  47.         elems = (E[]) new Object[maxlength];
  48.         clear();
  49.     }
  50.  
  51.     public boolean isEmpty () {
  52.         // Vrakja true ako i samo ako redicata e prazena.
  53.         return (length == 0);
  54.     }
  55.  
  56.     public int size () {
  57.         // Ja vrakja dolzinata na redicata.
  58.         return length;
  59.     }
  60.  
  61.     public E peek () {
  62.         // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  63.         if (length > 0)
  64.             return elems[front];
  65.         else
  66.             throw new NoSuchElementException();
  67.     }
  68.  
  69.     public void clear () {
  70.         // Ja prazni redicata.
  71.         length = 0;
  72.         front = rear = 0;  // arbitrary
  73.     }
  74.  
  75.     public void enqueue (E x) {
  76.         // Go dodava x na kraj od redicata.
  77.         elems[rear++] = x;
  78.         if (rear == elems.length)  rear = 0;
  79.         length++;
  80.     }
  81.  
  82.     public E dequeue () {
  83.         // Go otstranuva i vrakja pochetniot element na redicata.
  84.         if (length > 0) {
  85.             E frontmost = elems[front];
  86.             elems[front++] = null;
  87.             if (front == elems.length)  front = 0;
  88.             length--;
  89.             return frontmost;
  90.         } else
  91.             throw new NoSuchElementException();
  92.     }
  93. }
  94.  
  95. public class Kolokvium {
  96.  
  97.     public static void main(String[] args) throws IOException {
  98.        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  99.         String s = bf.readLine();
  100.         int k = Integer.parseInt(s);
  101.         s = bf.readLine();
  102.         int apsIMat=Integer.parseInt(s);
  103.         String[]elementi=new String[apsIMat];
  104.  
  105.  
  106.  
  107.         for(int i=0;i<apsIMat;i++)
  108.         {
  109.             elementi[i]=bf.readLine();
  110.         }
  111.  
  112.         int ostanati;
  113.         s = bf.readLine();
  114.         ostanati = Integer.parseInt(s);
  115.  
  116.         String[]elementi2=new String[ostanati];
  117.  
  118.         for(int i=0;i<ostanati;i++)
  119.         {
  120.             elementi2[i]=bf.readLine();
  121.             System.out.println(elementi2[i]);
  122.         }
  123.  
  124.         int mat;
  125.         s = bf.readLine();
  126.         mat = Integer.parseInt(s);
  127.  
  128.         String[]matematika=new String[mat];
  129.  
  130.         for(int i=0;i<mat;i++)
  131.         {
  132.             matematika[i]=bf.readLine();
  133.         }
  134.  
  135.         ArrayQueue<String> apsIMatR=new ArrayQueue<>(110);
  136.         ArrayQueue<String> tieStoNeIzlale=new ArrayQueue<>(110);
  137.         ArrayQueue<String> rezult=new ArrayQueue<>(k);
  138.  
  139.         ArrayQueue<String> tieStoIzlazale=new ArrayQueue<>(110);
  140.  
  141.         int termin=1;
  142.         System.out.println(termin);
  143.  
  144.         for(int i=0;i<apsIMat;i++)
  145.         {
  146.             int flag=0;
  147.             for(int j=0;j<mat;j++)
  148.             {
  149.                 if(elementi[i].equals(matematika[j])) {
  150.                     flag = 1;
  151.                     break;
  152.                 }
  153.             }
  154.             if(flag==1)
  155.             {
  156.                 if(rezult.size()<k)
  157.                 {
  158.                     rezult.enqueue(elementi[i]);
  159.                 }
  160.                 else{
  161.                     while(!rezult.isEmpty())
  162.                     {
  163.                         System.out.println(rezult.peek());
  164.                         rezult.dequeue();
  165.                     }
  166.                     rezult.clear();
  167.                     rezult.enqueue(elementi[i]);
  168.                     termin++;
  169.                     System.out.println(termin);
  170.  
  171.                 }
  172.             }
  173.             else
  174.             {
  175.                 tieStoIzlazale.enqueue(elementi[i]);
  176.             }
  177.         }
  178.  
  179.         for(int i=0;i<ostanati;i++)
  180.         {
  181.             if(rezult.size()<k)
  182.             {
  183.                 rezult.enqueue(elementi2[i]);
  184.             }
  185.             else
  186.             {
  187.                 while(!rezult.isEmpty())
  188.                 {
  189.                     System.out.println(rezult.peek());
  190.                     rezult.dequeue();
  191.                 }
  192.                 rezult.clear();
  193.                 rezult.enqueue(elementi2[i]);
  194.                 termin++;
  195.                 System.out.println(termin);
  196.  
  197.             }
  198.         }
  199.  
  200.         while(!tieStoIzlazale.isEmpty())
  201.         {
  202.             if(rezult.size()<k)
  203.             {
  204.  
  205.                 rezult.enqueue(tieStoIzlazale.peek());
  206.             }
  207.             else
  208.             {
  209.                 while(!rezult.isEmpty())
  210.                 {
  211.                     System.out.println(rezult.peek());
  212.                     rezult.dequeue();
  213.                 }
  214.                 rezult.clear();
  215.                 rezult.enqueue(tieStoIzlazale.peek());
  216.                 termin++;
  217.                 System.out.println(termin);
  218.  
  219.             }
  220.             tieStoIzlazale.dequeue();
  221.         }
  222.         while(!rezult.isEmpty()) {
  223.             System.out.println(rezult.peek());
  224.             rezult.dequeue();
  225.         }
  226.  
  227.     }
  228. }
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement