Advertisement
metalni

APS 2. Kolokvium Exercises Kumanovski Dijalekt

Jan 19th, 2021
1,748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  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 + "," + value + ">";
  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 void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  75.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  76.         int b = hash(key);
  77.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  78.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  79.                 // Make newEntry replace the existing entry ...
  80.                 curr.element = newEntry;
  81.                 return;
  82.             }
  83.         }
  84.         // Insert newEntry at the front of the 1WLL in bucket b ...
  85.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  86.     }
  87.  
  88.     public void delete(K key) {
  89.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  90.         int b = hash(key);
  91.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  92.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  93.                 if (pred == null)
  94.                     buckets[b] = curr.succ;
  95.                 else
  96.                     pred.succ = curr.succ;
  97.                 return;
  98.             }
  99.         }
  100.     }
  101.  
  102.     public String toString() {
  103.         String temp = "";
  104.         for (int i = 0; i < buckets.length; i++) {
  105.             temp += i + ":";
  106.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  107.                 temp += curr.element.toString() + " ";
  108.             }
  109.             temp += "\n";
  110.         }
  111.         return temp;
  112.     }
  113.  
  114. }
  115.  
  116. public class KumanovskiDijalekt {
  117.     public static void main (String[] args) throws IOException {
  118.  
  119.         BufferedReader br = new BufferedReader(new InputStreamReader(
  120.                 System.in));
  121.         int N = Integer.parseInt(br.readLine());
  122.  
  123.         CBHT<String, String> mapa = new CBHT<>(77);
  124.         String rechnik[]=new String[N];
  125.         for(int i=0;i<N;i++){
  126.             rechnik[i]=br.readLine();
  127.             String [] split = rechnik[i].split(" ");
  128.             mapa.insert(split[0], split[1]);
  129.         }
  130.  
  131.         String tekst=br.readLine();
  132.  
  133.         //Vasiot kod tuka
  134.         String [] split = tekst.split(" ");
  135.         String novTekst = "";
  136.  
  137.         for(int i=0; i<split.length; i++){
  138.             String searchWord = split[i];
  139.             char znak = split[i].charAt(split[i].length() - 1);
  140.             if(!Character.isAlphabetic(znak))
  141.                 searchWord = split[i].substring(0, split[i].length() - 1);
  142.             searchWord = searchWord.toLowerCase();
  143.             String newWord = split[i];
  144.  
  145.             if(mapa.search(searchWord) != null) {
  146.                 newWord = mapa.search(searchWord).element.value;
  147.                 if(!Character.isAlphabetic(znak))
  148.                     newWord += znak;
  149.                 if(Character.isUpperCase(split[i].charAt(0)))
  150.                     newWord = Character.toUpperCase(newWord.charAt(0)) + newWord.substring(1);
  151.             }
  152.  
  153.             novTekst += newWord + " ";
  154.         }
  155.         System.out.println(novTekst);
  156.     }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement