Advertisement
teodor_dalavera

Родендени

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