Advertisement
NenadKocev

[АПС] Родендени

Jan 18th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Rodendeni {
  6.  
  7.     public static boolean isPrime(int m){
  8.         for(int i = 2; i < Math.sqrt(m); i++)
  9.             if(m % i == 0)
  10.                 return false;
  11.  
  12.         return true;
  13.     }
  14.  
  15.     public static int findM(int n){
  16.         int m = (int)(n/0.6);
  17.  
  18.         while (!isPrime(m))
  19.             m++;
  20.         return m;
  21.     }
  22.  
  23.     public static void main(String[] args) throws IOException {
  24.     // write your code here
  25.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  26.         int n = Integer.parseInt(bufferedReader.readLine());
  27.         CBHT<Integer, String> lista = new CBHT<>(findM(n));
  28.         String [] human = new String[2];
  29.         String [] date = new String[3];
  30.  
  31.         for(int i = 0; i < n; i++){
  32.             human = bufferedReader.readLine().split("\\s+");
  33.             date = human[1].split("\\.");
  34.             int mesec = Integer.parseInt(date[1]);
  35.  
  36.             SLLNode<MapEntry<Integer, String>> h = lista.search(mesec);
  37.             if(h == null)
  38.                 lista.insert(mesec, human[0]);
  39.             else {
  40.                 String names = h.element.value;
  41.                 if(!names.contains(human[0]))
  42.                 names += " " + human[0];
  43.                 lista.insert(mesec, names);
  44.             }
  45.         }
  46.  
  47.         int s = Integer.parseInt(bufferedReader.readLine());
  48.         SLLNode<MapEntry<Integer, String>> h = lista.search(s);
  49.         if(h == null)
  50.             System.out.println("Nema");
  51.         else System.out.println(h.element.value);
  52.  
  53.     }
  54. }
  55.  
  56.  
  57. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  58.     // Each MapEntry object is a pair consisting of a key (a Comparable
  59. // object) and a value (an arbitrary object).
  60.     K key;
  61.     E value;
  62.     public MapEntry (K key, E val) {
  63.         this.key = key;
  64.         this.value = val;
  65.     }
  66.     public int compareTo (K that) {
  67. // Compare this map entry to that map entry.
  68.         @SuppressWarnings("unchecked")
  69.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  70.         return this.key.compareTo(other.key);
  71.     }
  72.     public String toString () {
  73.         return "<" + key + "," + value + ">";
  74.     }
  75. }
  76.  
  77. class CBHT<K extends Comparable<K>, E> {
  78.     private SLLNode<MapEntry<K, E>>[] buckets;
  79.  
  80.     @SuppressWarnings("unchecked")
  81.     public CBHT(int m) {
  82. // Construct an empty CBHT with m buckets.
  83.         buckets = (SLLNode<MapEntry<K, E>>[]) new
  84.                 SLLNode[m];
  85.     }
  86.  
  87.     private int hash(K key) {
  88. // Translate key to an index of the array buckets.
  89.         return Math.abs(key.hashCode()) % buckets.length;
  90.     }
  91.  
  92.     public SLLNode<MapEntry<K, E>> search(K targetKey) {
  93. // Find which if any node of this CBHT contains an
  94. // entry whose key is equal to targetKey.
  95. // Return a link to that node (or null if there is none).
  96.         int b = hash(targetKey);
  97.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr =
  98.                 curr.succ) {
  99.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  100.                 return curr;
  101.         }
  102.         return null;
  103.     }
  104.  
  105.     public void insert(K key, E val) {
  106. // Insert the entry <key, val> into this CBHT.
  107.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  108.         int b = hash(key);
  109.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr =
  110.                 curr.succ) {
  111.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  112. // Make newEntry replace the existing entry ...
  113.                 curr.element = newEntry;
  114.                 return;
  115.             }
  116.         }
  117. // Insert newEntry at the front of the 1WLL in bucket b ...
  118.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  119.     }
  120.  
  121.     public void delete(K key) {
  122. // Delete the entry (if any) whose key is equal
  123. // to key from this CBHT.
  124.         int b = hash(key);
  125.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b];
  126.              curr != null; pred = curr, curr = curr.succ) {
  127.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  128.                 if (pred == null)
  129.                     buckets[b] = curr.succ;
  130.                 else
  131.                     pred.succ = curr.succ;
  132.                 return;
  133.             }
  134.         }
  135.     }
  136. }
  137.  
  138. class SLLNode<E>{
  139.     E element;
  140.     SLLNode<E> succ;
  141.  
  142.     public SLLNode(E element, SLLNode<E> succ) {
  143.         this.element = element;
  144.         this.succ = succ;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement