Advertisement
brsjak

АПС - Родендени

Sep 18th, 2019
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.45 KB | None | 0 0
  1. /*
  2. Родендени Problem 1
  3.  
  4. Во заводот на статистика се прави ново истражување каде што се открива зависноста на месецот на раѓање со имињата на луѓето родени во тој месец. Ваша задача е за даден месец да ги прикажете сите различни имиња на луѓе родени во тој месец.
  5.  
  6. Влез: Во првиот ред од влезот е даден бројот на луѓе N (N<=10 000), а во секој нареден ред се дадени името на човекот и датумот на неговото раѓање разделени со празно место. Во последниот ред е даден месецот за кој треба да се прикажат сите различни имиња на луѓето родени во тој месец.
  7.  
  8. Излез: Листа со различни имиња на луѓе родени во дадениот месец. Доколку нема луѓе родени во тој месец да се испечати Nema.
  9.  
  10. Делумно решение: Задачата се смета за делумно решена доколку се поминати 3 тест примери.
  11.  
  12. Забелешка: При реализација на задачите не е дозволено да се користат помошни структури како низи или сл. На располагање од структурите имате само една хеш структура.
  13.  
  14. Име на класа (Јава):Rodendeni
  15.  
  16. Пример:
  17. 4
  18. Ivan 20.7.1976
  19. Ivan 16.7.1988
  20. Ana 18.7.1966
  21. Ivan 5.6.1988
  22. 7
  23. Ivan Ana
  24.  
  25. */
  26.  
  27. import java.util.*;
  28. import java.io.*;
  29.  
  30. class SLLNode<E> {
  31.     protected E element;
  32.     protected SLLNode<E> succ;
  33.  
  34.     public SLLNode(E elem, SLLNode<E> succ) {
  35.         this.element = elem;
  36.         this.succ = succ;
  37.     }
  38.  
  39.     @Override
  40.     public String toString() {
  41.         return element.toString();
  42.     }
  43. }
  44.  
  45. class SLL<E> {
  46.  
  47.     private SLLNode<E> first;
  48.  
  49.     public SLL() {
  50.         // Construct an empty SLL
  51.         this.first = null;
  52.     }
  53.  
  54.     public void deleteList() {
  55.         first = null;
  56.     }
  57.  
  58.     public int length() {
  59.         int ret;
  60.         if (first != null) {
  61.             SLLNode<E> tmp = first;
  62.             ret = 1;
  63.             while (tmp.succ != null) {
  64.                 tmp = tmp.succ;
  65.                 ret++;
  66.             }
  67.             return ret;
  68.         } else {
  69.             return 0;
  70.         }
  71.  
  72.     }
  73.  
  74.     @Override
  75.     public String toString() {
  76.         String ret = new String();
  77.         if (first != null) {
  78.             SLLNode<E> tmp = first;
  79.             ret += tmp + " ";
  80.             while (tmp.succ != null) {
  81.                 tmp = tmp.succ;
  82.                 ret += tmp + " ";
  83.             }
  84.         } else {
  85.             ret = "Prazna lista!!!";
  86.         }
  87.         return ret;
  88.     }
  89.  
  90.     public void insertFirst(E o) {
  91.         SLLNode<E> ins = new SLLNode<E>(o, first);
  92.         first = ins;
  93.     }
  94.  
  95.     public void insertAfter(E o, SLLNode<E> node) {
  96.         if (node != null) {
  97.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  98.             node.succ = ins;
  99.         } else {
  100.             System.out.println("Dadenot jazol e null");
  101.         }
  102.     }
  103.  
  104.     public void insertBefore(E o, SLLNode<E> before) {
  105.  
  106.         if (first != null) {
  107.             SLLNode<E> tmp = first;
  108.             if (first == before) {
  109.                 this.insertFirst(o);
  110.                 return;
  111.             }
  112.             // ako first!=before
  113.             while (tmp.succ != before) {
  114.                 tmp = tmp.succ;
  115.             }
  116.             if (tmp.succ == before) {
  117.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  118.                 tmp.succ = ins;
  119.             } else {
  120.                 System.out.println("Elementot ne postoi vo listata");
  121.             }
  122.         } else {
  123.             System.out.println("Listata e prazna");
  124.         }
  125.     }
  126.  
  127.     public void insertLast(E o) {
  128.         if (first != null) {
  129.             SLLNode<E> tmp = first;
  130.             while (tmp.succ != null) {
  131.                 tmp = tmp.succ;
  132.             }
  133.             SLLNode<E> ins = new SLLNode<E>(o, null);
  134.             tmp.succ = ins;
  135.         } else {
  136.             insertFirst(o);
  137.         }
  138.     }
  139.  
  140.     public E deleteFirst() {
  141.         if (first != null) {
  142.             SLLNode<E> tmp = first;
  143.             first = first.succ;
  144.             return tmp.element;
  145.         } else {
  146.             System.out.println("Listata e prazna");
  147.             return null;
  148.         }
  149.     }
  150.  
  151.     public E delete(SLLNode<E> node) {
  152.         if (first != null) {
  153.             SLLNode<E> tmp = first;
  154.             if (first == node) {
  155.                 return this.deleteFirst();
  156.             }
  157.             while (tmp.succ != node && tmp.succ.succ != null) {
  158.                 tmp = tmp.succ;
  159.             }
  160.             if (tmp.succ == node) {
  161.                 tmp.succ = tmp.succ.succ;
  162.                 return node.element;
  163.             } else {
  164.                 System.out.println("Elementot ne postoi vo listata");
  165.                 return null;
  166.             }
  167.         } else {
  168.             System.out.println("Listata e prazna");
  169.             return null;
  170.         }
  171.  
  172.     }
  173.  
  174.     public SLLNode<E> getFirst() {
  175.         return first;
  176.     }
  177.  
  178.     public SLLNode<E> find(E o) {
  179.         if (first != null) {
  180.             SLLNode<E> tmp = first;
  181.  
  182.             while (tmp.element != o && tmp.succ != null) {
  183.                 tmp = tmp.succ;
  184.             }
  185.             if (tmp.element == o) {
  186.                 return tmp;
  187.             } else {
  188.                 System.out.println("Elementot ne postoi vo listata");
  189.             }
  190.         } else {
  191.             System.out.println("Listata e prazna");
  192.         }
  193.         return first;
  194.     }
  195.  
  196.     public Iterator<E> iterator() {
  197.         // Return an iterator that visits all elements of this list, in left-to-right
  198.         // order.
  199.         return new LRIterator<E>();
  200.     }
  201.  
  202.     // //////////Inner class ////////////
  203.     private class LRIterator<E> implements Iterator<E> {
  204.  
  205.         private SLLNode<E> place, curr;
  206.  
  207.         private LRIterator() {
  208.             place = (SLLNode<E>) first;
  209.             curr = null;
  210.         }
  211.  
  212.         public boolean hasNext() {
  213.             return (place != null);
  214.         }
  215.  
  216.         public E next() {
  217.             if (place == null) {
  218.                 throw new NoSuchElementException();
  219.             }
  220.             E nextElem = place.element;
  221.             curr = place;
  222.             place = place.succ;
  223.             return nextElem;
  224.         }
  225.  
  226.         public void remove() {
  227.             // Not implemented
  228.         }
  229.     }
  230.  
  231.     public void mirror() {
  232.         if (first != null) {
  233.             // m=nextsucc, p=tmp,q=next
  234.             SLLNode<E> tmp = first;
  235.             SLLNode<E> newsucc = null;
  236.             SLLNode<E> next;
  237.  
  238.             while (tmp != null) {
  239.                 next = tmp.succ;
  240.                 tmp.succ = newsucc;
  241.                 newsucc = tmp;
  242.                 tmp = next;
  243.             }
  244.             first = newsucc;
  245.         }
  246.  
  247.     }
  248.  
  249.     public void merge(SLL<E> in) {
  250.         if (first != null) {
  251.             SLLNode<E> tmp = first;
  252.             while (tmp.succ != null) {
  253.                 tmp = tmp.succ;
  254.             }
  255.             tmp.succ = in.getFirst();
  256.         } else {
  257.             first = in.getFirst();
  258.         }
  259.     }
  260. }
  261.  
  262. public class Rodendeni {
  263.  
  264.     public static void main(String[] args) throws NumberFormatException, IOException {
  265.         // TODO Auto-generated method stub
  266.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  267.  
  268.         int n = Integer.parseInt(in.readLine());
  269.  
  270.         HashMap<Integer, SLL<Vraboten>> table = new HashMap<Integer, SLL<Vraboten>>(13);
  271.  
  272.         for (int i = 0; i < n; i++) {
  273.             SLL<Vraboten> lista = null;
  274.             String line = in.readLine();
  275.             String[] niza = line.split(" ");
  276.  
  277.             String ime = niza[0];
  278.             String datum = niza[1];
  279.  
  280.             Vraboten v = new Vraboten(ime, datum);
  281.  
  282.             if (table.containsKey(v.getMesec(v))) {
  283.                 lista = table.get(v.getMesec(v));
  284.                 lista.insertLast(v);
  285.                 table.put(v.getMesec(v), lista);
  286.             } else {
  287.                 lista = new SLL<Vraboten>();
  288.                 lista.insertLast(v);
  289.                 table.put(v.getMesec(v), lista);
  290.             }
  291.         }
  292.  
  293.         int m = Integer.parseInt(in.readLine());
  294.  
  295.         najdiRodendeni(table, m);
  296.     }
  297.  
  298.     public static HashMap<Integer, SLL<Vraboten>> brisiDuplikati(HashMap<Integer, SLL<Vraboten>> table, int n) {
  299.         SLL<Vraboten> lista = table.get(n);
  300.         SLLNode<Vraboten> dvizi = lista.getFirst();
  301.  
  302.         while (dvizi != null) {
  303.             SLLNode<Vraboten> dviziNapred = dvizi.succ;
  304.  
  305.             while (dviziNapred != null) {
  306.                 if (dvizi.element.compareTo(dviziNapred.element) == 1) {
  307.                     lista.delete(dviziNapred);
  308.                 }
  309.                 dviziNapred = dviziNapred.succ;
  310.             }
  311.             dvizi = dvizi.succ;
  312.         }
  313.  
  314.         return table;
  315.     }
  316.  
  317.     public static void najdiRodendeni(HashMap<Integer, SLL<Vraboten>> table, int n) {
  318.         if (table.containsKey(n)) {
  319.  
  320.             brisiDuplikati(table, n);
  321.  
  322.             SLL<Vraboten> lista = table.get(n);
  323.  
  324.             SLLNode<Vraboten> dvizi = lista.getFirst();
  325.  
  326.             while (dvizi != null) {
  327.                 System.out.print(dvizi.element.toString() + " ");
  328.                 dvizi = dvizi.succ;
  329.             }
  330.         } else {
  331.             System.out.println("Nema");
  332.         }
  333.     }
  334. }
  335.  
  336. class Vraboten implements Comparable<Vraboten> {
  337.     protected String ime;
  338.     protected String datum;
  339.  
  340.     public Vraboten() {
  341.     }
  342.  
  343.     public Vraboten(String ime, String datum) {
  344.         this.ime = ime;
  345.         this.datum = datum;
  346.     }
  347.  
  348.     public int getMesec(Vraboten v) {
  349.         String[] datum = v.datum.split("\\.");
  350.         return Integer.parseInt(datum[1]);
  351.     }
  352.  
  353.     @Override
  354.     public int compareTo(Vraboten v) {
  355.         if (ime.equals(v.ime)) {
  356.             return 1;
  357.         }
  358.         return 0;
  359.     }
  360.  
  361.     public String toString() {
  362.         return ime;
  363.  
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement