Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Rodendeni {
- /*
- public static void main(String args[]) {
- Scanner scanner = new Scanner(System.in);
- int numOfPeople = Integer.parseInt(scanner.nextLine());
- // Month -> List of Names
- Map<Integer, List<String>> map = new HashMap<>();
- for (int i = 0; i < numOfPeople; i++) {
- String[] split = scanner.nextLine().split("\\s+");
- String name = split[0];
- Integer month = Integer.parseInt(split[1].split("\\.")[1]);
- //System.out.println(name + " " + month);
- map.computeIfAbsent(month, key -> new ArrayList<>()).add(name);
- map.computeIfPresent(month, (integer, strings) -> strings).add(name);
- }
- //System.out.println(map);
- Integer target = Integer.parseInt(scanner.nextLine());
- //System.out.println(target);
- String collect = Optional.ofNullable(map.get(target))
- .orElse(new ArrayList<>())
- .stream()
- .distinct()
- .collect(Collectors.joining(" "));
- if (collect.isEmpty()) System.out.println("Nema");
- else System.out.println(collect);
- }
- */
- public static void main(String args[]) {
- String text = "10\n" +
- "Bojan 19.6.1988\n" +
- "Bojan 19.7.1978\n" +
- "Bojan 19.7.1988\n" +
- "Bojana 19.6.1988\n" +
- "Bojan 21.6.1988\n" +
- "Bojan 22.7.1988\n" +
- "Bojan 24.7.1988\n" +
- "Bojan 27.7.1988\n" +
- "Ana 19.7.1987\n" +
- "Bojana 19.6.1988\n" +
- "6";
- /** Proper output:
- * Ana Ivana Simon Ognen Leona Martin
- * */
- Scanner scanner = new Scanner(System.in);
- //Scanner scanner = new Scanner(text);
- int numOfPeople = Integer.parseInt(scanner.nextLine());
- // Month -> List of Names
- CBHT<Integer, String> map = new CBHT<>(numOfPeople * 2);
- for (int i = 0; i < numOfPeople; i++) {
- String[] split = scanner.nextLine().split("\\s+");
- String name = split[0];
- Integer month = Integer.parseInt(split[1].split("\\.")[1]);
- map.insertLast(month, name);
- }
- //System.out.println(map);
- Integer target = Integer.parseInt(scanner.nextLine());
- SLLNode<MapEntry<Integer, String>> current = map.searchList(target);
- if (current == null) {
- System.out.println("Nema");
- return;
- }
- while (current != null){
- System.out.print(current.element.value + " ");
- current = current.succ;
- }
- }
- }
- /**
- * Во заводот на статистика се прави ново истражување каде што се открива зависноста на месецот на раѓање со имињата на луѓето родени во тој месец.
- * Ваша задача е за даден месец да ги прикажете сите различни имиња на луѓе родени во тој месец.
- * <p>
- * Влез: Во првиот ред од влезот е даден бројот на луѓе N (N<=10 000), а во секој нареден ред се дадени името на човекот и датумот на неговото раѓање разделени со празно место. Во последниот ред е даден месецот за кој треба да се прикажат сите различни имиња на луѓето родени во тој месец.
- * <p>
- * Излез: Листа со различни имиња на луѓе родени во дадениот месец. Доколку нема луѓе родени во тој месец да се испечати Nema.
- * <p>
- * Делумно решение: Задачата се смета за делумно решена доколку се поминати 3 тест примери.
- * <p>
- * Забелешка: При реализација на задачите не е дозволено да се користат помошни структури како низи или сл. На располагање од структурите имате само една хеш структура.
- * <p>
- * Име на класа (Јава):Rodendeni
- * <p>
- * Пример:
- * 4
- * Ivan 20.7.1976
- * Ivan 16.7.1988
- * Ana 18.7.1966
- * Ivan 5.6.1988
- * 7
- * Ivan Ana
- */
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable
- // object) and a value (an arbitrary object).
- K key;
- E value;
- public MapEntry(K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo(K that) {
- // Compare this map entry to that map entry.
- @SuppressWarnings("unchecked")
- MapEntry<K, E> other = (MapEntry<K, E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString() {
- return "<" + key + "," + value + ">";
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- private SLLNode<MapEntry<K, E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K, E>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
- return curr;
- }
- return null;
- }
- public E searchValue(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
- return curr.element.value;
- }
- return null;
- }
- public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(curr.element.key)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
- }
- public void insertFirst(K key, E val) {
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- /**
- * Prepend element at the begging of the SLL, O(1)
- *
- * public void insertFirst(E o) {
- * SLLNode<E> ins = new SLLNode<E>(o, first);
- * first = ins;
- * }
- * */
- SLLNode<MapEntry<K, E>> insert = new SLLNode<>(newEntry, buckets[b]);
- buckets[b] = insert;
- }
- public void insertLast(K key, E val) {
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- /**
- * Prepend element at the begging of the SLL, O(1)
- *
- * public void insertLast(E o) {
- * if (first != null) {
- * SLLNode<E> tmp = first;
- * while (tmp.succ != null)
- * tmp = tmp.succ;
- * SLLNode<E> ins = new SLLNode<E>(o, null);
- * tmp.succ = ins;
- * } else {
- * insertFirst(o);
- * }
- * }
- * */
- if (buckets[b] != null){
- SLLNode<MapEntry<K,E>> temp = buckets[b];
- if (temp.element.value.equals(val)){ // No Duplicates for first
- return;
- }
- while (temp.succ != null) {
- if (temp.element.value.equals(val)){ // No Duplicates for rest
- return;
- }
- temp = temp.succ;
- }
- if (temp.element.value.equals(val)){ // No Duplicates for last
- return;
- }
- SLLNode<MapEntry<K,E>> insert = new SLLNode<>(newEntry, null);
- temp.succ = insert;
- } else {
- insertFirst(key, val);
- }
- }
- public SLLNode<MapEntry<K, E>> searchList(K targetKey) {
- int b = hash(targetKey);
- return buckets[b];
- }
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this CBHT.
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public String toString() {
- StringBuilder temp = new StringBuilder();
- for (int i = 0; i < buckets.length; i++) {
- temp.append(i).append(":");
- for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp.append(curr.element.value.toString()).append(" ");
- }
- temp.append("\n");
- }
- return temp.toString();
- }
- }
- // NOT USED AT ALL
- class OBHT<K extends Comparable<K>, E> {
- // An object of class OBHT is an open-bucket hash table, containing entries
- // of class MapEntry.
- private MapEntry<K, E>[] buckets;
- // buckets[b] is null if bucket b has never been occupied.
- // buckets[b] is former if bucket b is formerly-occupied
- // by an entry that has since been deleted (and not yet replaced).
- static final int NONE = -1; // ... distinct from any bucket index.
- private static final MapEntry former = new MapEntry(null, null);
- // This guarantees that, for any genuine entry e,
- // e.key.equals(former.key) returns false.
- private int occupancy = 0;
- // ... number of occupied or formerly-occupied buckets in this OBHT.
- @SuppressWarnings("unchecked")
- public OBHT(int m) {
- // Construct an empty OBHT with m buckets.
- buckets = (MapEntry<K, E>[]) new MapEntry[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public int search(K targetKey) {
- // Find which if any bucket of this OBHT is occupied by an entry whose key
- // is equal to targetKey. Return the index of that bucket.
- int b = hash(targetKey);
- int n_search = 0;
- for (; ; ) {
- MapEntry<K, E> oldEntry = buckets[b];
- if (oldEntry == null)
- return NONE;
- else if (targetKey.equals(oldEntry.key))
- return b;
- else {
- b = (b + 1) % buckets.length;
- n_search++;
- if (n_search == buckets.length)
- return NONE;
- }
- }
- }
- public void insert(K key, E val) {
- // Insert the entry <key, val> into this OBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- int n_search = 0;
- for (; ; ) {
- MapEntry<K, E> oldEntry = buckets[b];
- if (oldEntry == null) {
- if (++occupancy == buckets.length) {
- System.out.println("Hash tabelata e polna!!!");
- }
- buckets[b] = newEntry;
- return;
- } else if (oldEntry == former
- || key.equals(oldEntry.key)) {
- buckets[b] = newEntry;
- return;
- } else {
- b = (b + 1) % buckets.length;
- n_search++;
- if (n_search == buckets.length)
- return;
- }
- }
- }
- @SuppressWarnings("unchecked")
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this OBHT.
- int b = hash(key);
- int n_search = 0;
- for (; ; ) {
- MapEntry<K, E> oldEntry = buckets[b];
- if (oldEntry == null)
- return;
- else if (key.equals(oldEntry.key)) {
- buckets[b] = former;//(MapEntry<K,E>)former;
- return;
- } else {
- b = (b + 1) % buckets.length;
- n_search++;
- if (n_search == buckets.length)
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- if (buckets[i] == null)
- temp += "\n";
- else if (buckets[i] == former)
- temp += "former\n";
- else
- temp += buckets[i] + "\n";
- }
- return temp;
- }
- public OBHT<K, E> clone() {
- OBHT<K, E> copy = new OBHT<K, E>(buckets.length);
- for (int i = 0; i < buckets.length; i++) {
- MapEntry<K, E> e = buckets[i];
- if (e != null && e != former)
- copy.buckets[i] = new MapEntry<K, E>(e.key, e.value);
- else
- copy.buckets[i] = e;
- }
- return copy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment