Latkoski

Pomosnici 2

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