NikolaPython

Hashing_KumanovskiDijalekt

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