Filip_Markoski

[ADSx] - Birthdays

Jan 15th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.67 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Rodendeni {
  4.     /*
  5.     public static void main(String args[]) {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.         int numOfPeople = Integer.parseInt(scanner.nextLine());
  9.  
  10.         // Month -> List of Names
  11.         Map<Integer, List<String>> map = new HashMap<>();
  12.         for (int i = 0; i < numOfPeople; i++) {
  13.             String[] split = scanner.nextLine().split("\\s+");
  14.             String name = split[0];
  15.             Integer month = Integer.parseInt(split[1].split("\\.")[1]);
  16.  
  17.             //System.out.println(name + " " + month);
  18.  
  19.             map.computeIfAbsent(month, key -> new ArrayList<>()).add(name);
  20.             map.computeIfPresent(month, (integer, strings) -> strings).add(name);
  21.  
  22.         }
  23.  
  24.         //System.out.println(map);
  25.  
  26.         Integer target = Integer.parseInt(scanner.nextLine());
  27.         //System.out.println(target);
  28.         String collect = Optional.ofNullable(map.get(target))
  29.                 .orElse(new ArrayList<>())
  30.                 .stream()
  31.                 .distinct()
  32.                 .collect(Collectors.joining(" "));
  33.         if (collect.isEmpty()) System.out.println("Nema");
  34.         else System.out.println(collect);
  35.  
  36.     }
  37.     */
  38.     public static void main(String args[]) {
  39.  
  40.         String text = "10\n" +
  41.                 "Bojan 19.6.1988\n" +
  42.                 "Bojan 19.7.1978\n" +
  43.                 "Bojan 19.7.1988\n" +
  44.                 "Bojana 19.6.1988\n" +
  45.                 "Bojan 21.6.1988\n" +
  46.                 "Bojan 22.7.1988\n" +
  47.                 "Bojan 24.7.1988\n" +
  48.                 "Bojan 27.7.1988\n" +
  49.                 "Ana 19.7.1987\n" +
  50.                 "Bojana 19.6.1988\n" +
  51.                 "6";
  52.  
  53.         /** Proper output:
  54.          * Ana Ivana Simon Ognen Leona Martin
  55.          * */
  56.  
  57.         Scanner scanner = new Scanner(System.in);
  58.         //Scanner scanner = new Scanner(text);
  59.         int numOfPeople = Integer.parseInt(scanner.nextLine());
  60.  
  61.         // Month -> List of Names
  62.         CBHT<Integer, String> map = new CBHT<>(numOfPeople * 2);
  63.         for (int i = 0; i < numOfPeople; i++) {
  64.             String[] split = scanner.nextLine().split("\\s+");
  65.             String name = split[0];
  66.             Integer month = Integer.parseInt(split[1].split("\\.")[1]);
  67.  
  68.             map.insertLast(month, name);
  69.  
  70.         }
  71.  
  72.         //System.out.println(map);
  73.  
  74.         Integer target = Integer.parseInt(scanner.nextLine());
  75.  
  76.         SLLNode<MapEntry<Integer, String>> current = map.searchList(target);
  77.         if (current == null) {
  78.             System.out.println("Nema");
  79.             return;
  80.         }
  81.  
  82.         while (current != null){
  83.             System.out.print(current.element.value + " ");
  84.             current = current.succ;
  85.         }
  86.  
  87.     }
  88. }
  89.  
  90. /**
  91.  * Во заводот на статистика се прави ново истражување каде што се открива зависноста на месецот на раѓање со имињата на луѓето родени во тој месец.
  92.  * Ваша задача е за даден месец да ги прикажете сите различни имиња на луѓе родени во тој месец.
  93.  * <p>
  94.  * Влез: Во првиот ред од влезот е даден бројот на луѓе N (N<=10 000), а во секој нареден ред се дадени името на човекот и датумот на неговото раѓање разделени со празно место. Во последниот ред е даден месецот за кој треба да се прикажат сите различни имиња на луѓето родени во тој месец.
  95.  * <p>
  96.  * Излез: Листа со различни имиња на луѓе родени во дадениот месец. Доколку нема луѓе родени во тој месец да се испечати Nema.
  97.  * <p>
  98.  * Делумно решение: Задачата се смета за делумно решена доколку се поминати 3 тест примери.
  99.  * <p>
  100.  * Забелешка: При реализација на задачите не е дозволено да се користат помошни структури како низи или сл. На располагање од структурите имате само една хеш структура.
  101.  * <p>
  102.  * Име на класа (Јава):Rodendeni
  103.  * <p>
  104.  * Пример:
  105.  * 4
  106.  * Ivan 20.7.1976
  107.  * Ivan 16.7.1988
  108.  * Ana 18.7.1966
  109.  * Ivan 5.6.1988
  110.  * 7
  111.  * Ivan Ana
  112.  */
  113.  
  114. class SLLNode<E> {
  115.     protected E element;
  116.     protected SLLNode<E> succ;
  117.  
  118.     public SLLNode(E elem, SLLNode<E> succ) {
  119.         this.element = elem;
  120.         this.succ = succ;
  121.     }
  122.  
  123.     @Override
  124.     public String toString() {
  125.         return element.toString();
  126.     }
  127. }
  128.  
  129. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  130.  
  131.     // Each MapEntry object is a pair consisting of a key (a Comparable
  132.     // object) and a value (an arbitrary object).
  133.     K key;
  134.     E value;
  135.  
  136.     public MapEntry(K key, E val) {
  137.         this.key = key;
  138.         this.value = val;
  139.     }
  140.  
  141.     public int compareTo(K that) {
  142.         // Compare this map entry to that map entry.
  143.         @SuppressWarnings("unchecked")
  144.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  145.         return this.key.compareTo(other.key);
  146.     }
  147.  
  148.     public String toString() {
  149.         return "<" + key + "," + value + ">";
  150.     }
  151. }
  152.  
  153. class CBHT<K extends Comparable<K>, E> {
  154.  
  155.     // An object of class CBHT is a closed-bucket hash table, containing
  156.     // entries of class MapEntry.
  157.     private SLLNode<MapEntry<K, E>>[] buckets;
  158.  
  159.     @SuppressWarnings("unchecked")
  160.     public CBHT(int m) {
  161.         // Construct an empty CBHT with m buckets.
  162.         buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
  163.     }
  164.  
  165.     private int hash(K key) {
  166.         // Translate key to an index of the array buckets.
  167.         return Math.abs(key.hashCode()) % buckets.length;
  168.     }
  169.  
  170.     public SLLNode<MapEntry<K, E>> search(K targetKey) {
  171.         // Find which if any node of this CBHT contains an entry whose key is
  172.         // equal
  173.         // to targetKey. Return a link to that node (or null if there is none).
  174.         int b = hash(targetKey);
  175.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  176.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  177.                 return curr;
  178.         }
  179.         return null;
  180.     }
  181.  
  182.     public E searchValue(K targetKey) {
  183.         // Find which if any node of this CBHT contains an entry whose key is
  184.         // equal
  185.         // to targetKey. Return a link to that node (or null if there is none).
  186.         int b = hash(targetKey);
  187.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  188.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  189.                 return curr.element.value;
  190.         }
  191.         return null;
  192.     }
  193.  
  194.     public void insert(K key, E val) {        // Insert the entry <key, val> into this CBHT.
  195.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  196.         int b = hash(key);
  197.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  198.             if (key.equals(curr.element.key)) {
  199.                 // Make newEntry replace the existing entry ...
  200.                 curr.element = newEntry;
  201.                 return;
  202.             }
  203.         }
  204.         // Insert newEntry at the front of the 1WLL in bucket b ...
  205.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  206.     }
  207.  
  208.     public void insertFirst(K key, E val) {
  209.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  210.         int b = hash(key);
  211.         /**
  212.          * Prepend element at the begging of the SLL, O(1)
  213.          *
  214.          * public void insertFirst(E o) {
  215.          *      SLLNode<E> ins = new SLLNode<E>(o, first);
  216.          *      first = ins;
  217.          * }
  218.          * */
  219.  
  220.         SLLNode<MapEntry<K, E>> insert = new SLLNode<>(newEntry, buckets[b]);
  221.         buckets[b] = insert;
  222.     }
  223.  
  224.  
  225.  
  226.     public void insertLast(K key, E val) {
  227.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  228.         int b = hash(key);
  229.         /**
  230.          * Prepend element at the begging of the SLL, O(1)
  231.          *
  232.          * public void insertLast(E o) {
  233.          *      if (first != null) {
  234.          *          SLLNode<E> tmp = first;
  235.          *          while (tmp.succ != null)
  236.          *          tmp = tmp.succ;
  237.          *          SLLNode<E> ins = new SLLNode<E>(o, null);
  238.          *          tmp.succ = ins;
  239.          *      } else {
  240.          *          insertFirst(o);
  241.          *      }
  242.          * }
  243.          * */
  244.  
  245.         if (buckets[b] != null){
  246.             SLLNode<MapEntry<K,E>> temp = buckets[b];
  247.             if (temp.element.value.equals(val)){ // No Duplicates for first
  248.                 return;
  249.             }
  250.             while (temp.succ != null) {
  251.                 if (temp.element.value.equals(val)){ // No Duplicates for rest
  252.                     return;
  253.                 }
  254.                 temp = temp.succ;
  255.             }
  256.             if (temp.element.value.equals(val)){ // No Duplicates for last
  257.                 return;
  258.             }
  259.             SLLNode<MapEntry<K,E>> insert = new SLLNode<>(newEntry, null);
  260.             temp.succ = insert;
  261.         } else {
  262.             insertFirst(key, val);
  263.         }
  264.     }
  265.  
  266.     public SLLNode<MapEntry<K, E>> searchList(K targetKey) {
  267.         int b = hash(targetKey);
  268.         return buckets[b];
  269.     }
  270.  
  271.     public void delete(K key) {
  272.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  273.         int b = hash(key);
  274.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  275.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  276.                 if (pred == null)
  277.                     buckets[b] = curr.succ;
  278.                 else
  279.                     pred.succ = curr.succ;
  280.                 return;
  281.             }
  282.         }
  283.     }
  284.  
  285.     public String toString() {
  286.         StringBuilder temp = new StringBuilder();
  287.         for (int i = 0; i < buckets.length; i++) {
  288.             temp.append(i).append(":");
  289.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  290.                 temp.append(curr.element.value.toString()).append(" ");
  291.             }
  292.             temp.append("\n");
  293.         }
  294.         return temp.toString();
  295.     }
  296.  
  297. }
  298.  
  299.  
  300. // NOT USED AT ALL
  301. class OBHT<K extends Comparable<K>, E> {
  302.  
  303.     // An object of class OBHT is an open-bucket hash table, containing entries
  304.     // of class MapEntry.
  305.     private MapEntry<K, E>[] buckets;
  306.  
  307.     // buckets[b] is null if bucket b has never been occupied.
  308.     // buckets[b] is former if bucket b is formerly-occupied
  309.     // by an entry that has since been deleted (and not yet replaced).
  310.  
  311.     static final int NONE = -1; // ... distinct from any bucket index.
  312.  
  313.     private static final MapEntry former = new MapEntry(null, null);
  314.     // This guarantees that, for any genuine entry e,
  315.     // e.key.equals(former.key) returns false.
  316.  
  317.     private int occupancy = 0;
  318.     // ... number of occupied or formerly-occupied buckets in this OBHT.
  319.  
  320.     @SuppressWarnings("unchecked")
  321.     public OBHT(int m) {
  322.         // Construct an empty OBHT with m buckets.
  323.         buckets = (MapEntry<K, E>[]) new MapEntry[m];
  324.     }
  325.  
  326.  
  327.     private int hash(K key) {
  328.         // Translate key to an index of the array buckets.
  329.         return Math.abs(key.hashCode()) % buckets.length;
  330.     }
  331.  
  332.  
  333.     public int search(K targetKey) {
  334.         // Find which if any bucket of this OBHT is occupied by an entry whose key
  335.         // is equal to targetKey. Return the index of that bucket.
  336.         int b = hash(targetKey);
  337.         int n_search = 0;
  338.         for (; ; ) {
  339.             MapEntry<K, E> oldEntry = buckets[b];
  340.             if (oldEntry == null)
  341.                 return NONE;
  342.             else if (targetKey.equals(oldEntry.key))
  343.                 return b;
  344.             else {
  345.                 b = (b + 1) % buckets.length;
  346.                 n_search++;
  347.                 if (n_search == buckets.length)
  348.                     return NONE;
  349.  
  350.             }
  351.         }
  352.     }
  353.  
  354.  
  355.     public void insert(K key, E val) {
  356.         // Insert the entry <key, val> into this OBHT.
  357.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  358.         int b = hash(key);
  359.         int n_search = 0;
  360.         for (; ; ) {
  361.             MapEntry<K, E> oldEntry = buckets[b];
  362.             if (oldEntry == null) {
  363.                 if (++occupancy == buckets.length) {
  364.                     System.out.println("Hash tabelata e polna!!!");
  365.                 }
  366.                 buckets[b] = newEntry;
  367.                 return;
  368.             } else if (oldEntry == former
  369.                     || key.equals(oldEntry.key)) {
  370.                 buckets[b] = newEntry;
  371.                 return;
  372.             } else {
  373.                 b = (b + 1) % buckets.length;
  374.                 n_search++;
  375.                 if (n_search == buckets.length)
  376.                     return;
  377.  
  378.             }
  379.         }
  380.     }
  381.  
  382.  
  383.     @SuppressWarnings("unchecked")
  384.     public void delete(K key) {
  385.         // Delete the entry (if any) whose key is equal to key from this OBHT.
  386.         int b = hash(key);
  387.         int n_search = 0;
  388.         for (; ; ) {
  389.             MapEntry<K, E> oldEntry = buckets[b];
  390.  
  391.             if (oldEntry == null)
  392.                 return;
  393.             else if (key.equals(oldEntry.key)) {
  394.                 buckets[b] = former;//(MapEntry<K,E>)former;
  395.                 return;
  396.             } else {
  397.                 b = (b + 1) % buckets.length;
  398.                 n_search++;
  399.                 if (n_search == buckets.length)
  400.                     return;
  401.  
  402.             }
  403.         }
  404.     }
  405.  
  406.  
  407.     public String toString() {
  408.         String temp = "";
  409.         for (int i = 0; i < buckets.length; i++) {
  410.             temp += i + ":";
  411.             if (buckets[i] == null)
  412.                 temp += "\n";
  413.             else if (buckets[i] == former)
  414.                 temp += "former\n";
  415.             else
  416.                 temp += buckets[i] + "\n";
  417.         }
  418.         return temp;
  419.     }
  420.  
  421.  
  422.     public OBHT<K, E> clone() {
  423.         OBHT<K, E> copy = new OBHT<K, E>(buckets.length);
  424.         for (int i = 0; i < buckets.length; i++) {
  425.             MapEntry<K, E> e = buckets[i];
  426.             if (e != null && e != former)
  427.                 copy.buckets[i] = new MapEntry<K, E>(e.key, e.value);
  428.             else
  429.                 copy.buckets[i] = e;
  430.         }
  431.         return copy;
  432.     }
  433. }
Advertisement
Add Comment
Please, Sign In to add comment