Advertisement
Guest User

Untitled

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