Advertisement
brsjak

АПС - Статистика

Sep 18th, 2019
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.37 KB | None | 0 0
  1. /*
  2. Max likes football very much. In order to check the popuplarity of the game, he decided to talk to random people and ask them about their favourite game and note it down in a list.
  3.  
  4. Given a list of name of people and their favourite sport, help Max in finding the sport liked by most of the people, and also how many people like football.
  5.  
  6. He could have met same people more than once, he will only count response of his first meet with any person.
  7.  
  8. Note : Name of person as well as sport is a single string in lowercase. Length of name of people as well as sport is less than 11.
  9.  
  10. Input :: First line will contain no of entries in the list. i.e. N Next N lines will contain two strings, person's name and sports he like.
  11.  
  12. Output :: In first ine, name of sport liked by most no of people (In case of more than one games is liked by highest no of people, output the first one in lexicographical order). In second line, no of people having football as their favourite game.
  13.  
  14. Constraints: 1<=N<=100000 1<=Name of person<=10 1<=Name of sport<=10
  15.  
  16.  
  17. SAMPLE INPUT
  18.  
  19. 5
  20. Filip Odbojka
  21. Frose Kosarka
  22. Pedza Odbojka
  23. Vlado Kosarka
  24. Davor Fudbal
  25.  
  26.  
  27. SAMPLE OUTPUT
  28. NAJPOPULAREN SPORT: odbojka
  29. FUDBAL SAKAAT: 1
  30. */
  31.  
  32. import java.util.*;
  33. import java.io.*;
  34.  
  35. class SLLNode<E> {
  36.     protected E element;
  37.     protected SLLNode<E> succ;
  38.  
  39.     public SLLNode(E elem, SLLNode<E> succ) {
  40.         this.element = elem;
  41.         this.succ = succ;
  42.     }
  43.  
  44.     @Override
  45.     public String toString() {
  46.         return element.toString();
  47.     }
  48. }
  49.  
  50. class SLL<E> {
  51.  
  52.     private SLLNode<E> first;
  53.  
  54.     public SLL() {
  55.         // Construct an empty SLL
  56.         this.first = null;
  57.     }
  58.  
  59.     public void deleteList() {
  60.         first = null;
  61.     }
  62.  
  63.     public int length() {
  64.         int ret;
  65.         if (first != null) {
  66.             SLLNode<E> tmp = first;
  67.             ret = 1;
  68.             while (tmp.succ != null) {
  69.                 tmp = tmp.succ;
  70.                 ret++;
  71.             }
  72.             return ret;
  73.         } else {
  74.             return 0;
  75.         }
  76.  
  77.     }
  78.  
  79.     @Override
  80.     public String toString() {
  81.         String ret = new String();
  82.         if (first != null) {
  83.             SLLNode<E> tmp = first;
  84.             ret += tmp + " ";
  85.             while (tmp.succ != null) {
  86.                 tmp = tmp.succ;
  87.                 ret += tmp + " ";
  88.             }
  89.         } else {
  90.             ret = "Prazna lista!!!";
  91.         }
  92.         return ret;
  93.     }
  94.  
  95.     public void insertFirst(E o) {
  96.         SLLNode<E> ins = new SLLNode<E>(o, first);
  97.         first = ins;
  98.     }
  99.  
  100.     public void insertAfter(E o, SLLNode<E> node) {
  101.         if (node != null) {
  102.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  103.             node.succ = ins;
  104.         } else {
  105.             System.out.println("Dadenot jazol e null");
  106.         }
  107.     }
  108.  
  109.     public void insertBefore(E o, SLLNode<E> before) {
  110.  
  111.         if (first != null) {
  112.             SLLNode<E> tmp = first;
  113.             if (first == before) {
  114.                 this.insertFirst(o);
  115.                 return;
  116.             }
  117.             // ako first!=before
  118.             while (tmp.succ != before) {
  119.                 tmp = tmp.succ;
  120.             }
  121.             if (tmp.succ == before) {
  122.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  123.                 tmp.succ = ins;
  124.             } else {
  125.                 System.out.println("Elementot ne postoi vo listata");
  126.             }
  127.         } else {
  128.             System.out.println("Listata e prazna");
  129.         }
  130.     }
  131.  
  132.     public void insertLast(E o) {
  133.         if (first != null) {
  134.             SLLNode<E> tmp = first;
  135.             while (tmp.succ != null) {
  136.                 tmp = tmp.succ;
  137.             }
  138.             SLLNode<E> ins = new SLLNode<E>(o, null);
  139.             tmp.succ = ins;
  140.         } else {
  141.             insertFirst(o);
  142.         }
  143.     }
  144.  
  145.     public E deleteFirst() {
  146.         if (first != null) {
  147.             SLLNode<E> tmp = first;
  148.             first = first.succ;
  149.             return tmp.element;
  150.         } else {
  151.             System.out.println("Listata e prazna");
  152.             return null;
  153.         }
  154.     }
  155.  
  156.     public E delete(SLLNode<E> node) {
  157.         if (first != null) {
  158.             SLLNode<E> tmp = first;
  159.             if (first == node) {
  160.                 return this.deleteFirst();
  161.             }
  162.             while (tmp.succ != node && tmp.succ.succ != null) {
  163.                 tmp = tmp.succ;
  164.             }
  165.             if (tmp.succ == node) {
  166.                 tmp.succ = tmp.succ.succ;
  167.                 return node.element;
  168.             } else {
  169.                 System.out.println("Elementot ne postoi vo listata");
  170.                 return null;
  171.             }
  172.         } else {
  173.             System.out.println("Listata e prazna");
  174.             return null;
  175.         }
  176.  
  177.     }
  178.  
  179.     public SLLNode<E> getFirst() {
  180.         return first;
  181.     }
  182.  
  183.     public SLLNode<E> find(E o) {
  184.         if (first != null) {
  185.             SLLNode<E> tmp = first;
  186.  
  187.             while (tmp.element != o && tmp.succ != null) {
  188.                 tmp = tmp.succ;
  189.             }
  190.             if (tmp.element == o) {
  191.                 return tmp;
  192.             } else {
  193.                 System.out.println("Elementot ne postoi vo listata");
  194.             }
  195.         } else {
  196.             System.out.println("Listata e prazna");
  197.         }
  198.         return first;
  199.     }
  200.  
  201.     public Iterator<E> iterator() {
  202.         // Return an iterator that visits all elements of this list, in left-to-right
  203.         // order.
  204.         return new LRIterator<E>();
  205.     }
  206.  
  207.     // //////////Inner class ////////////
  208.     private class LRIterator<E> implements Iterator<E> {
  209.  
  210.         private SLLNode<E> place, curr;
  211.  
  212.         private LRIterator() {
  213.             place = (SLLNode<E>) first;
  214.             curr = null;
  215.         }
  216.  
  217.         public boolean hasNext() {
  218.             return (place != null);
  219.         }
  220.  
  221.         public E next() {
  222.             if (place == null) {
  223.                 throw new NoSuchElementException();
  224.             }
  225.             E nextElem = place.element;
  226.             curr = place;
  227.             place = place.succ;
  228.             return nextElem;
  229.         }
  230.  
  231.         public void remove() {
  232.             // Not implemented
  233.         }
  234.     }
  235.  
  236.     public void mirror() {
  237.         if (first != null) {
  238.             // m=nextsucc, p=tmp,q=next
  239.             SLLNode<E> tmp = first;
  240.             SLLNode<E> newsucc = null;
  241.             SLLNode<E> next;
  242.  
  243.             while (tmp != null) {
  244.                 next = tmp.succ;
  245.                 tmp.succ = newsucc;
  246.                 newsucc = tmp;
  247.                 tmp = next;
  248.             }
  249.             first = newsucc;
  250.         }
  251.  
  252.     }
  253.  
  254.     public void merge(SLL<E> in) {
  255.         if (first != null) {
  256.             SLLNode<E> tmp = first;
  257.             while (tmp.succ != null) {
  258.                 tmp = tmp.succ;
  259.             }
  260.             tmp.succ = in.getFirst();
  261.         } else {
  262.             first = in.getFirst();
  263.         }
  264.     }
  265. }
  266.  
  267. public class SportskaStatistika {
  268.  
  269.     public static void main(String[] args) throws NumberFormatException, IOException {
  270.         // TODO Auto-generated method stub
  271.  
  272.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  273.  
  274.         int n = Integer.parseInt(in.readLine());
  275.  
  276.         HashMap<String, SLL<Covek>> table = new HashMap<String, SLL<Covek>>(2 * n + 1);
  277.  
  278.         for (int i = 0; i < n; i++) {
  279.  
  280.             SLL<Covek> lista = null;
  281.  
  282.             String line = in.readLine();
  283.             String[] niza = line.split(" ");
  284.  
  285.             Covek c = new Covek(niza[0], niza[1]);
  286.  
  287.             if (table.containsKey(c.sport)) {
  288.                 lista = table.get(c.sport);
  289.                 lista.insertLast(c);
  290.                 table.put(c.sport, lista);
  291.             } else {
  292.                 lista = new SLL<Covek>();
  293.                 lista.insertLast(c);
  294.                 table.put(c.sport, lista);
  295.             }
  296.         }
  297.         brisiDuplikati(table);
  298.         String najpopularenKluc = najpopularenSport(table);
  299.         System.out.println("NAJPOPULAREN SPORT: " + najpopularenKluc);
  300.         System.out.println("FUDBAL SAKAAT: " + najdiFudbal(table));
  301.  
  302.     }
  303.  
  304.     public static int najdiFudbal(HashMap<String, SLL<Covek>> table) {
  305.         SLL<Covek> lista = table.get("fudbal");
  306.         if(lista==null) {
  307.             return 0;
  308.         }
  309.         SLLNode<Covek> dvizi = lista.getFirst();
  310.         int count = 0;
  311.  
  312.         while (dvizi != null) {
  313.             count++;
  314.             dvizi = dvizi.succ;
  315.         }
  316.         return count;
  317.     }
  318.  
  319.     public static String najpopularenSport(HashMap<String, SLL<Covek>> table) {
  320.         Set<String> klucovi = table.keySet();
  321.         String[] klucevi = klucovi.toArray(new String[klucovi.size()]);
  322.         int max = 0;
  323.         String maxKey = null;
  324.         for (String s : klucevi) {
  325.             int count = 0;
  326.             SLL<Covek> lista = table.get(s);
  327.             SLLNode<Covek> dvizi = lista.getFirst();
  328.             while (dvizi != null) {
  329.                 count++;
  330.                 dvizi = dvizi.succ;
  331.             }
  332.             if (count >= max) {
  333.                 max = count;
  334.                 maxKey = s;
  335.             }
  336.  
  337.         }
  338.         return maxKey;
  339.     }
  340.  
  341.     public static HashMap<String, SLL<Covek>> brisiDuplikati(HashMap<String, SLL<Covek>> table) {
  342.         Set<String> klucovi = table.keySet();
  343.         String[] klucevi = klucovi.toArray(new String[klucovi.size()]);
  344.  
  345.         for (String s : klucevi) {
  346.  
  347.             SLL<Covek> lista = table.get(s);
  348.             SLLNode<Covek> dvizi = lista.getFirst();
  349.  
  350.             while (dvizi != null) {
  351.                 SLLNode<Covek> dviziNapred = dvizi.succ;
  352.  
  353.                 while (dviziNapred != null) {
  354.                     if (dvizi.element.compareTo(dviziNapred.element) == 1) {
  355.                         lista.delete(dviziNapred);
  356.                     }
  357.                     dviziNapred = dviziNapred.succ;
  358.                 }
  359.                 dvizi = dvizi.succ;
  360.             }
  361.         }
  362.         return table;
  363.     }
  364.  
  365. }
  366.  
  367. class Covek implements Comparable<Covek> {
  368.     protected String ime;
  369.     protected String sport;
  370.  
  371.     public Covek() {
  372.     }
  373.  
  374.     public Covek(String ime, String sport) {
  375.         this.ime = ime;
  376.         this.sport = sport.toLowerCase();
  377.     }
  378.  
  379.     @Override
  380.     public String toString() {
  381.         return ime;
  382.     }
  383.  
  384.     @Override
  385.     public int compareTo(Covek c) {
  386.         if (ime.equals(c.ime)) {
  387.             return 1;
  388.         }
  389.         return 0;
  390.     }
  391.  
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement