kristina7

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

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