Advertisement
Guest User

KumanovskIDijalekt

a guest
Dec 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. package kumanovski_dijalekt;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. class SLLNode<E> {
  7. protected E element;
  8. protected SLLNode<E> succ;
  9.  
  10. public SLLNode(E elem, SLLNode<E> succ) {
  11. this.element = elem;
  12. this.succ = succ;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return element.toString();
  18. }
  19. }
  20. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  21.  
  22. // Each MapEntry object is a pair consisting of a key (a Comparable
  23. // object) and a value (an arbitrary object).
  24. K key;
  25. E value;
  26.  
  27. public MapEntry (K key, E val) {
  28. this.key = key;
  29. this.value = val;
  30. }
  31.  
  32. public int compareTo (K that) {
  33. // Compare this map entry to that map entry.
  34. @SuppressWarnings("unchecked")
  35. MapEntry<K,E> other = (MapEntry<K,E>) that;
  36. return this.key.compareTo(other.key);
  37. }
  38.  
  39. public String toString () {
  40. return "<" + key + "," + value + ">";
  41. }
  42. }
  43. class CBHT<K extends Comparable<K>, E> {
  44.  
  45. // An object of class CBHT is a closed-bucket hash table, containing
  46. // entries of class MapEntry.
  47. private SLLNode<MapEntry<K,E>>[] buckets;
  48.  
  49. @SuppressWarnings("unchecked")
  50. public CBHT(int m) {
  51. // Construct an empty CBHT with m buckets.
  52. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  53. }
  54.  
  55. private int hash(K key) {
  56. // Translate key to an index of the array buckets.
  57. return Math.abs(key.hashCode()) % buckets.length;
  58. }
  59.  
  60. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  61. // Find which if any node of this CBHT contains an entry whose key is
  62. // equal
  63. // to targetKey. Return a link to that node (or null if there is none).
  64. int b = hash(targetKey);
  65. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  66. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  67. return curr;
  68. }
  69. return null;
  70. }
  71.  
  72. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  73. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  74. int b = hash(key);
  75. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  76. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  77. // Make newEntry replace the existing entry ...
  78. curr.element = newEntry;
  79. return;
  80. }
  81. }
  82. // Insert newEntry at the front of the 1WLL in bucket b ...
  83. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  84. }
  85.  
  86. public void delete(K key) {
  87. // Delete the entry (if any) whose key is equal to key from this CBHT.
  88. int b = hash(key);
  89. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  90. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  91. if (pred == null)
  92. buckets[b] = curr.succ;
  93. else
  94. pred.succ = curr.succ;
  95. return;
  96. }
  97. }
  98. }
  99.  
  100. public String toString() {
  101. String temp = "";
  102. for (int i = 0; i < buckets.length; i++) {
  103. temp += i + ":";
  104. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  105. temp += curr.element.toString() + " ";
  106. }
  107. temp += "\n";
  108. }
  109. return temp;
  110. }
  111. }
  112. public class Kumanovski_Dijalekt {
  113. public static void main(String[] args){
  114. Scanner sc = new Scanner(System.in);
  115. Integer n = Integer.parseInt(sc.nextLine());
  116. CBHT<String,String> hesh =new CBHT<>(2*n);
  117.  
  118. for(int i=0;i<n;i++){
  119. String vnesi = sc.nextLine();
  120. String[] parts= vnesi.split(" ");
  121. hesh.insert(parts[0], parts[1]);
  122. }
  123.  
  124. String tekst = sc.nextLine();
  125. int flag=0;
  126. int flag1=0;
  127. String[] recenica = tekst.split(" ");
  128. ArrayList<String> recenicaPrevedena= new ArrayList<>();
  129. for(int i=0;i<recenica.length;i++){
  130. String zbor = recenica[i];
  131.  
  132. String prvaBukva=zbor.substring(0,1);
  133.  
  134. char[] c =prvaBukva.toCharArray();
  135.  
  136. if(Character.isUpperCase(c[0])){
  137. flag1=1;
  138. }
  139.  
  140. zbor=zbor.toLowerCase();
  141.  
  142. String posledenZnak=new String();
  143. if(zbor.endsWith(",") || zbor.endsWith(",")
  144. || zbor.endsWith("!") || zbor.endsWith("?")){
  145. posledenZnak=zbor.substring(zbor.length()-1,zbor.length());
  146. zbor = zbor.substring(0,zbor.length()-1);
  147. flag=1;
  148. }
  149.  
  150. SLLNode<MapEntry<String,String>> node = hesh.search(zbor);
  151.  
  152.  
  153. if(node!=null){
  154. if(node.element.value.equals(zbor)){
  155. if(flag==1){
  156.  
  157. if(flag1==1){
  158. String novZbor= node.element.value.substring(1, node.element.value.length());
  159. recenicaPrevedena.add(prvaBukva+ novZbor +posledenZnak+" ");
  160. flag1=0;
  161. }else
  162. recenicaPrevedena.add(node.element.value +posledenZnak+" ");
  163. flag=0;
  164. }else
  165. recenicaPrevedena.add(node.element.value +" ");
  166. }else{
  167. if(flag==1){
  168.  
  169. if(flag1==1){
  170. String novZbor= node.element.value.substring(1, node.element.value.length());
  171.  
  172. recenicaPrevedena.add(prvaBukva+ novZbor +posledenZnak+" ");
  173. flag1=0;
  174. }else
  175. recenicaPrevedena.add(node.element.value +posledenZnak+" ");
  176. flag=0;
  177. }else{
  178. if(flag1==1){
  179. String novZbor= node.element.value.substring(1, node.element.value.length());
  180. prvaBukva= node.element.value.substring(0,1).toUpperCase();
  181. recenicaPrevedena.add(prvaBukva+ novZbor +posledenZnak+" ");
  182. flag1=0;
  183. }else
  184. recenicaPrevedena.add(node.element.value+ " ");
  185. }
  186. }
  187.  
  188. }else{
  189. if(flag==1){
  190. if(flag1==1){
  191. String novZbor= zbor.substring(1, zbor.length());
  192.  
  193. recenicaPrevedena.add(prvaBukva+ novZbor +posledenZnak+" ");
  194. flag1=0;
  195. }else
  196. recenicaPrevedena.add(zbor + posledenZnak+" ");
  197. flag=0;
  198. }else{
  199. if(flag1==1){
  200. String novZbor= zbor.substring(1, zbor.length());
  201.  
  202. recenicaPrevedena.add(prvaBukva+ novZbor +posledenZnak+" ");
  203. flag1=0;
  204. }else
  205. recenicaPrevedena.add(zbor + " ");
  206. }
  207. }
  208. }
  209.  
  210. for (String l : recenicaPrevedena){
  211. System.out.print(l);
  212. }
  213. }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement