Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  7.  
  8.     // Each MapEntry object is a pair consisting of a key (a Comparable
  9.     // object) and a value (an arbitrary object).
  10.     K key;
  11.     E value;
  12.  
  13.     public MapEntry (K key, E val) {
  14.         this.key = key;
  15.         this.value = val;
  16.     }
  17.    
  18.     public int compareTo (K that) {
  19.     // Compare this map entry to that map entry.
  20.         @SuppressWarnings("unchecked")
  21.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  22.         return this.key.compareTo(other.key);
  23.     }
  24.  
  25.     public String toString () {
  26.         return "<" + key + "," + value + ">";
  27.     }
  28. }
  29.  
  30. class SLLNode<E> {
  31.     protected E element;
  32.     protected SLLNode<E> succ;
  33.  
  34.     public SLLNode(E elem, SLLNode<E> succ) {
  35.         this.element = elem;
  36.         this.succ = succ;
  37.     }
  38.  
  39.     @Override
  40.     public String toString() {
  41.         return element.toString();
  42.     }
  43. }
  44.  
  45. class CBHT<K extends Comparable<K>, E> {
  46.  
  47.     // An object of class CBHT is a closed-bucket hash table, containing
  48.     // entries of class MapEntry.
  49.     private SLLNode<MapEntry<K,E>>[] buckets;
  50.  
  51.     @SuppressWarnings("unchecked")
  52.     public CBHT(int m) {
  53.         // Construct an empty CBHT with m buckets.
  54.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  55.     }
  56.  
  57.     private int hash(K key) {
  58.         // Translate key to an index of the array buckets.
  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.  
  117. public class Zborovi {
  118.     public static void main(String[] args) throws IOException {
  119.        
  120.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  121.         int N = Integer.parseInt(br.readLine());
  122.    
  123.         String[] recnik = new String[N];
  124.         for(int i = 0; i < N; ++i){
  125.             recnik[i] = br.readLine();
  126.         }
  127.        
  128.         CBHT<String,String> tabela = new CBHT<String,String>(N + 1);
  129.         for(int i = 0; i < N; ++i){
  130.             String[] input = recnik[i].split(" ");
  131.             tabela.insert(input[0], input[1]);
  132.         }
  133.        
  134.         String text = br.readLine();
  135.         String tekst = "";
  136.        
  137.         String[] newText = text.split(" ");
  138.         String output = "";
  139.         for(int i = 0; i < newText.length; ++i){
  140.             String word = newText[i];
  141.             String interpukcija = "";
  142.            
  143.             if(word.contains(",") ||word.contains(".") || word.contains("!") || word.contains("?")){
  144.                 word = word.substring(0,newText[i].length() - 1);
  145.             }
  146.             SLLNode<MapEntry<String, String>> del = tabela.search(word);
  147.            
  148.             if(del == null){
  149.                 tekst = newText[i];
  150.             } else
  151.                 tekst = newText[i].replace(word, del.element.value);
  152.             output += tekst + interpukcija + " ";
  153.         }
  154.             System.out.print(output);
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement