Advertisement
teodor_dalavera

Кумановски дијалект

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