Latkoski

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

Jan 21st, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.53 KB | None | 0 0
  1. package kumanovski;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. import javax.xml.transform.Templates;
  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 SLLNode<E> {
  33.     protected E element;
  34.     protected SLLNode<E> succ;
  35.  
  36.     public SLLNode(E elem, SLLNode<E> succ) {
  37.         this.element = elem;
  38.         this.succ = succ;
  39.     }
  40.  
  41.     @Override
  42.     public String toString() {
  43.         return element.toString();
  44.     }
  45. }
  46.  
  47. class CBHT<K extends Comparable<K>, E> {
  48.  
  49.     // An object of class CBHT is a closed-bucket hash table, containing
  50.     // entries of class MapEntry.
  51.     private SLLNode<MapEntry<K,E>>[] buckets;
  52.  
  53.     @SuppressWarnings("unchecked")
  54.     public CBHT(int m) {
  55.         // Construct an empty CBHT with m buckets.
  56.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  57.     }
  58.  
  59.     private int hash(K key) {
  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.     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.         int n = (int) (N * 1.5);
  123.         CBHT<String, String> recnik = new CBHT<String, String>(n);
  124.         String rechnik[] = new String[N];
  125.         for (int i = 0; i < N; i++) {
  126.             rechnik[i] = br.readLine();
  127.             String[] pom = rechnik[i].split(" ");
  128.             recnik.insert(pom[0].toLowerCase(), pom[1]);
  129.         }
  130.             String tekst = br.readLine();
  131.             String[] zborovi = tekst.split(" ");
  132.         if(N == 0)
  133.             {
  134.                 System.out.print(tekst);
  135.                 return;
  136.             }
  137.             for (int i = 0; i < zborovi.length; i++) {
  138.                 String znak = null;
  139.                 String word = zborovi[i];
  140.                 if (zborovi[i].contains(".")) {
  141.                     word = zborovi[i].replace(".", "");
  142.                     znak = ".";
  143.                 }
  144.                 if (zborovi[i].contains(",")) {
  145.                     word = zborovi[i].replace(",", "");
  146.                     znak = ",";
  147.                 }
  148.                 if (zborovi[i].contains("!")) {
  149.                     word = zborovi[i].replace("!", "");
  150.                     znak = "!";
  151.                 }
  152.                 if (zborovi[i].contains("?")) {
  153.                     word = zborovi[i].replace("?", "");
  154.                     znak = "?";
  155.                 }
  156.  
  157.                 SLLNode<MapEntry<String, String>> node = recnik.search(word.toLowerCase());
  158.                 if (node == null) {
  159.                    
  160.                         if (i + 1 == zborovi.length) {
  161.                             System.out.print(zborovi[i]);
  162.                         } else
  163.                             System.out.print(zborovi[i] + " ");
  164.                 }
  165.                 else
  166.                 {
  167.                     if((i> 0&&zborovi[i-1].contains(".")) || i==0)
  168.                     {
  169.                         String zbor = node.element.value.substring(0, 1).toUpperCase();
  170.                         String soGolema = node.element.value.substring(1, node.element.value.length());
  171.                         word = zbor + soGolema;
  172.                     }
  173.                     else
  174.                         word = node.element.value;
  175.                     if(znak != null)
  176.                     {
  177.                         if(i+1 == zborovi.length)
  178.                           System.out.print(word + znak);
  179.                         else
  180.                           System.out.print(word + znak + " ");
  181.                     }
  182.                     else
  183.                     {
  184.                         if(i+1 == zborovi.length)
  185.                           System.out.print(word);
  186.                         else
  187.                           System.out.print(word + " ");
  188.                     }  
  189.                 }
  190.             }
  191.     }
  192.    
  193. }
Advertisement
Add Comment
Please, Sign In to add comment