Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- interface Queue<E> {
- // Elementi na redicata se objekti od proizvolen tip.
- // Metodi za pristap:
- public boolean isEmpty ();
- // Vrakja true ako i samo ako redicata e prazena.
- public int size ();
- // Ja vrakja dolzinata na redicata.
- public E peek ();
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- // Metodi za transformacija:
- public void clear ();
- // Ja prazni redicata.
- public void enqueue (E x);
- // Go dodava x na kraj od redicata.
- public E dequeue ();
- // Go otstranuva i vrakja pochetniot element na redicata.
- }
- class ArrayQueue<E> implements Queue<E> {
- // Redicata e pretstavena na sledniot nacin:
- // length go sodrzi brojot na elementi.
- // Ako length > 0, togash elementite na redicata se zachuvani vo elems[front...rear-1]
- // Ako rear > front, togash vo elems[front...maxlength-1] i elems[0...rear-1]
- E[] elems;
- int length, front, rear;
- // Konstruktor ...
- @SuppressWarnings("unchecked")
- public ArrayQueue (int maxlength) {
- elems = (E[]) new Object[maxlength];
- clear();
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako redicata e prazena.
- return (length == 0);
- }
- public int size () {
- // Ja vrakja dolzinata na redicata.
- return length;
- }
- public E peek () {
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- if (length > 0)
- return elems[front];
- else
- throw new NoSuchElementException();
- }
- public void clear () {
- // Ja prazni redicata.
- length = 0;
- front = rear = 0; // arbitrary
- }
- public void enqueue (E x) {
- // Go dodava x na kraj od redicata.
- elems[rear++] = x;
- if (rear == elems.length) rear = 0;
- length++;
- }
- public E dequeue () {
- // Go otstranuva i vrakja pochetniot element na redicata.
- if (length > 0) {
- E frontmost = elems[front];
- elems[front++] = null;
- if (front == elems.length) front = 0;
- length--;
- return frontmost;
- } else
- throw new NoSuchElementException();
- }
- }
- public class Kolokvium {
- public static void main(String[] args) throws IOException {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- String s = bf.readLine();
- int k = Integer.parseInt(s);
- s = bf.readLine();
- int apsIMat=Integer.parseInt(s);
- String[]elementi=new String[apsIMat];
- for(int i=0;i<apsIMat;i++)
- {
- elementi[i]=bf.readLine();
- }
- int ostanati;
- s = bf.readLine();
- ostanati = Integer.parseInt(s);
- String[]elementi2=new String[ostanati];
- for(int i=0;i<ostanati;i++)
- {
- elementi2[i]=bf.readLine();
- System.out.println(elementi2[i]);
- }
- int mat;
- s = bf.readLine();
- mat = Integer.parseInt(s);
- String[]matematika=new String[mat];
- for(int i=0;i<mat;i++)
- {
- matematika[i]=bf.readLine();
- }
- ArrayQueue<String> apsIMatR=new ArrayQueue<>(110);
- ArrayQueue<String> tieStoNeIzlale=new ArrayQueue<>(110);
- ArrayQueue<String> rezult=new ArrayQueue<>(k);
- ArrayQueue<String> tieStoIzlazale=new ArrayQueue<>(110);
- int termin=1;
- System.out.println(termin);
- for(int i=0;i<apsIMat;i++)
- {
- int flag=0;
- for(int j=0;j<mat;j++)
- {
- if(elementi[i].equals(matematika[j])) {
- flag = 1;
- break;
- }
- }
- if(flag==1)
- {
- if(rezult.size()<k)
- {
- rezult.enqueue(elementi[i]);
- }
- else{
- while(!rezult.isEmpty())
- {
- System.out.println(rezult.peek());
- rezult.dequeue();
- }
- rezult.clear();
- rezult.enqueue(elementi[i]);
- termin++;
- System.out.println(termin);
- }
- }
- else
- {
- tieStoIzlazale.enqueue(elementi[i]);
- }
- }
- for(int i=0;i<ostanati;i++)
- {
- if(rezult.size()<k)
- {
- rezult.enqueue(elementi2[i]);
- }
- else
- {
- while(!rezult.isEmpty())
- {
- System.out.println(rezult.peek());
- rezult.dequeue();
- }
- rezult.clear();
- rezult.enqueue(elementi2[i]);
- termin++;
- System.out.println(termin);
- }
- }
- while(!tieStoIzlazale.isEmpty())
- {
- if(rezult.size()<k)
- {
- rezult.enqueue(tieStoIzlazale.peek());
- }
- else
- {
- while(!rezult.isEmpty())
- {
- System.out.println(rezult.peek());
- rezult.dequeue();
- }
- rezult.clear();
- rezult.enqueue(tieStoIzlazale.peek());
- termin++;
- System.out.println(termin);
- }
- tieStoIzlazale.dequeue();
- }
- while(!rezult.isEmpty()) {
- System.out.println(rezult.peek());
- rezult.dequeue();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement