Advertisement
brsjak

АПС - Кумановски Дијалект CBHT

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