Filip_Markoski

[ADSx] - KumanovskiDijalekt

Jan 11th, 2018
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  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.toString() + "," + value.toString() + ">";
  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.         // 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 E searchValue(K targetKey) {
  75.         // Find which if any node of this CBHT contains an entry whose key is
  76.         // equal
  77.         // to targetKey. Return a link to that node (or null if there is none).
  78.         int b = hash(targetKey);
  79.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  80.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  81.                 return curr.element.value;
  82.         }
  83.         return null;
  84.     }
  85.  
  86.     public void insert(K key, E val) {        // Insert the entry <key, val> into this CBHT.
  87.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  88.         int b = hash(key);
  89.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  90.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  91.                 // Make newEntry replace the existing entry ...
  92.                 curr.element = newEntry;
  93.                 return;
  94.             }
  95.         }
  96.         // Insert newEntry at the front of the 1WLL in bucket b ...
  97.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  98.     }
  99.  
  100.     public void delete(K key) {
  101.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  102.         int b = hash(key);
  103.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  104.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  105.                 if (pred == null)
  106.                     buckets[b] = curr.succ;
  107.                 else
  108.                     pred.succ = curr.succ;
  109.                 return;
  110.             }
  111.         }
  112.     }
  113.  
  114.     public String toString() {
  115.         StringBuffer sb = new StringBuffer();
  116.         for (int i = 0; i < buckets.length; i++) {
  117.             sb.append(i).append(":");
  118.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  119.                 sb
  120.                         .append(curr.element.key)
  121.                         .append(" -> ")
  122.                         .append(curr.element.value)
  123.                         .append(" ");
  124.             }
  125.             sb.append("\n");
  126.         }
  127.         return sb.toString();
  128.     }
  129.  
  130. }
  131.  
  132. public class KumanovskiDijalekt {
  133.     public static void main(String[] args) throws IOException {
  134.  
  135.         BufferedReader br = new BufferedReader(new InputStreamReader(
  136.                 System.in));
  137.         int N = Integer.parseInt(br.readLine());
  138.  
  139.         String rechnik[] = new String[N];
  140.         for (int i = 0; i < N; i++) {
  141.             rechnik[i] = br.readLine();
  142.         }
  143.  
  144.         String tekst = br.readLine();
  145.  
  146.         if (N == 0){
  147.             System.out.println(tekst);
  148.             return;
  149.         }
  150.  
  151.         // Kumanovo -> Macedonian
  152.         CBHT<String, String> cbht = new CBHT<>(2*N);
  153.  
  154.         for (int i = 0; i < rechnik.length; i++) {
  155.             String split[] = rechnik[i].split("\\s+");
  156.             cbht.insert(split[0], split[1]);
  157.         }
  158.  
  159.         //System.out.println(cbht.toString());
  160.  
  161.         StringBuffer sb = new StringBuffer();
  162.  
  163.         Scanner scanner = new Scanner(tekst);
  164.         while (scanner.hasNext()) {
  165.             String word = scanner.next();
  166.             String translation = cbht.searchValue(trim(word).toLowerCase());
  167.             if (translation == null) {
  168.                 sb.append(word).append(" ");
  169.             } else {
  170.                 Character last = word.charAt(word.length() - 1);
  171.                 if (!Character.isAlphabetic(last)){
  172.                     translation = translation + last;
  173.                 }
  174.                 if (Character.isUpperCase(word.charAt(0))){
  175.                     translation = capitalize(translation);
  176.                 }
  177.                 sb.append(translation).append(" ");
  178.             }
  179.         }
  180.         System.out.println(sb.toString().trim());
  181.     }
  182.  
  183.     public static String trim(String str) {
  184.         char last = str.charAt(str.length() - 1);
  185.         if (!Character.isAlphabetic(last))
  186.             return str.substring(0, str.length() - 1);
  187.         return str;
  188.     }
  189.  
  190.     public static String capitalize(String word) {
  191.         if (Character.isUpperCase(word.charAt(0)))
  192.             return word; // word is already capitalized
  193.         return word.substring(0, 1).toUpperCase() + word.substring(1);
  194.     }
  195. }
  196. /**
  197.  * You are given a dictionary of words in the kumanovo dialect, and how they are written in the macedonian language. Then, you are given a text written in the kumanovo dialect. You need to swap all occurences of a dialect word with the corresponding standard macedonian word.
  198.  * <p>
  199.  * Note: You should avoid the punctuation marks, dot (.) , comma (,), exclamation point (!) and question mark (?). The swapped words should keep the case they were originally written in (case sensitive).
  200.  */
Advertisement
Add Comment
Please, Sign In to add comment