Advertisement
Guest User

Kumanovski Dijalekt

a guest
Jan 25th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Hashtable;
  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.  
  31. class SLLNode<E> {
  32. protected E element;
  33. protected SLLNode<E> succ;
  34.  
  35. public SLLNode(E elem, SLLNode<E> succ) {
  36. this.element = elem;
  37. this.succ = succ;
  38. }
  39.  
  40. @Override
  41. public String toString() {
  42. return element.toString();
  43. }
  44. }
  45.  
  46. class CBHT<K extends Comparable<K>, E> {
  47.  
  48. // An object of class CBHT is a closed-bucket hash table, containing
  49. // entries of class MapEntry.
  50. private SLLNode<MapEntry<K,E>>[] buckets;
  51.  
  52. @SuppressWarnings("unchecked")
  53. public CBHT(int m) {
  54. // Construct an empty CBHT with m buckets.
  55. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  56. }
  57.  
  58.  
  59.  
  60. private int hash(K key) {
  61. // Napishete ja vie HASH FUNKCIJATA
  62.  
  63. return Math.abs(key.hashCode()) % buckets.length;
  64. }
  65.  
  66. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  67. // Find which if any node of this CBHT contains an entry whose key is
  68. // equal
  69. // to targetKey. Return a link to that node (or null if there is none).
  70. int b = hash(targetKey);
  71. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  72. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  73. return curr;
  74. }
  75. return null;
  76. }
  77.  
  78. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  79. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  80. int b = hash(key);
  81. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  82. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  83. // Make newEntry replace the existing entry ...
  84. curr.element = newEntry;
  85. return;
  86. }
  87. }
  88. // Insert newEntry at the front of the 1WLL in bucket b ...
  89. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  90. }
  91.  
  92. public void delete(K key) {
  93. // Delete the entry (if any) whose key is equal to key from this CBHT.
  94. int b = hash(key);
  95. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  96. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  97. if (pred == null)
  98. buckets[b] = curr.succ;
  99. else
  100. pred.succ = curr.succ;
  101. return;
  102. }
  103. }
  104. }
  105.  
  106. public String toString() {
  107. String temp = "";
  108. for (int i = 0; i < buckets.length; i++) {
  109. temp += i + ":";
  110. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  111. temp += curr.element.toString() + " ";
  112. }
  113. temp += "\n";
  114. }
  115. return temp;
  116. }
  117.  
  118. }
  119.  
  120. public class KumanovskiDijalekt {
  121. public static void main(String[] args) throws IOException {
  122.  
  123. BufferedReader br = new BufferedReader(new InputStreamReader(
  124. System.in));
  125. int N = Integer.parseInt(br.readLine());
  126.  
  127. String rechnik[] = new String[N];
  128.  
  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.  
  137. CBHT<String, String> table = new CBHT<>(N);
  138. char znak=' ';
  139. String[] pomniza;
  140. for (int i = 0; i < N; i++) {
  141. pomniza = rechnik[i].split(" ");
  142. table.insert(pomniza[0], pomniza[1]);
  143. }
  144. String[] zborchinja = tekst.split(" ");
  145. StringBuilder s = new StringBuilder();
  146. boolean flag = false;
  147. if(N==0)
  148. System.out.println(tekst);
  149. else{
  150. for (int i = 0; i < zborchinja.length; i++) {
  151. StringBuilder a = new StringBuilder();
  152. if (zborchinja[i].contains(",") || zborchinja[i].contains(".") || zborchinja[i].contains("!") || zborchinja[i].contains("?")) {
  153. znak = zborchinja[i].charAt(zborchinja[i].length()-1);
  154. zborchinja[i] = zborchinja[i].substring(0, zborchinja[i].length() - 1);
  155. }
  156.  
  157.  
  158. if (table.search(zborchinja[i].toLowerCase()) == null) {
  159. s.append(zborchinja[i]);
  160. if (znak != ' '){
  161. s.append(znak);
  162. znak = ' ';
  163. }
  164. s.append(" ");
  165. }
  166. else
  167. {
  168. SLLNode<MapEntry<String,String>> pom = table.search(zborchinja[i].toLowerCase());
  169. if(Character.isUpperCase(zborchinja[i].charAt(0))){
  170. flag = true;
  171. }
  172. if(flag) {
  173. String temp = pom.element.value;
  174. a.append(Character.toUpperCase(temp.charAt(0)));
  175. temp = temp.substring(1,temp.length());
  176. a.append(temp);
  177. flag = false;
  178. s.append(a.toString());
  179. }
  180. else
  181. s.append(pom.element.value);
  182. if (znak != ' '){
  183. s.append(znak);
  184. znak = ' ';
  185. }
  186. s.append(" ");
  187. }
  188. }
  189. }
  190. System.out.println(s.toString());
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement