Advertisement
mvCode

Kumanovski Dijalekt AIPS

Jan 18th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.lang.Character;
  5. import java.lang.String;
  6.  
  7. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  8.  
  9. // Each MapEntry object is a pair consisting of a key (a Comparable
  10. // object) and a value (an arbitrary object).
  11. K key;
  12. E value;
  13.  
  14. public MapEntry (K key, E val) {
  15. this.key = key;
  16. this.value = val;
  17. }
  18.  
  19. public int compareTo (K that) {
  20. // Compare this map entry to that map entry.
  21. @SuppressWarnings("unchecked")
  22. MapEntry<K,E> other = (MapEntry<K,E>) that;
  23. return this.key.compareTo(other.key);
  24. }
  25.  
  26. public String toString () {
  27. return "<" + key + "," + value + ">";
  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. private int hash(K key) {
  59. // Napishete ja vie HASH FUNKCIJATA
  60. String str = key.toString();
  61. char letters[] = str.toCharArray();
  62. int num = 0;
  63.  
  64. for ( int i = 0; i < letters.length; i++ ){
  65. num += ( int ) letters[i] * i * i;
  66. }
  67. num = num % buckets.length;
  68. return num;
  69. }
  70.  
  71. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  72. // Find which if any node of this CBHT contains an entry whose key is
  73. // equal
  74. // to targetKey. Return a link to that node (or null if there is none).
  75. int b = hash(targetKey);
  76. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  77. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  78. return curr;
  79. }
  80. return null;
  81. }
  82.  
  83. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  84. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  85. int b = hash(key);
  86. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  87. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  88. // Make newEntry replace the existing entry ...
  89. curr.element = newEntry;
  90. return;
  91. }
  92. }
  93. // Insert newEntry at the front of the 1WLL in bucket b ...
  94. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  95. }
  96.  
  97. public void delete(K key) {
  98. // Delete the entry (if any) whose key is equal to key from this CBHT.
  99. int b = hash(key);
  100. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  101. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  102. if (pred == null)
  103. buckets[b] = curr.succ;
  104. else
  105. pred.succ = curr.succ;
  106. return;
  107. }
  108. }
  109. }
  110.  
  111. public String toString() {
  112. String temp = "";
  113. for (int i = 0; i < buckets.length; i++) {
  114. temp += i + ":";
  115. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  116. temp += curr.element.toString() + " ";
  117. }
  118. temp += "\n";
  119. }
  120. return temp;
  121. }
  122.  
  123. }
  124.  
  125. public class KumanovskiDijalekt {
  126. public static void main (String[] args) throws IOException {
  127.  
  128. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  129. int N = Integer.parseInt(br.readLine());
  130.  
  131. String rechnik[]=new String[N];
  132. for(int i=0;i<N;i++){
  133. rechnik[i]=br.readLine();
  134. }
  135.  
  136. String tekst=br.readLine();
  137.  
  138. //Vasiot kod tuka
  139.  
  140. CBHT< String, String > table = new CBHT<>( 2 * N + 1);
  141. for( int i = 0; i < N; i++ ){
  142.  
  143. String words[] = rechnik[i].split( " " );
  144. table.insert( words[0], words[1] );
  145. } // hash tabelata e gotova
  146.  
  147. String words[] = tekst.split( " " );
  148.  
  149. for( int i = 0; i < words.length; i++ ){ // gi sreduva zborovite po red
  150.  
  151. char firstLetter = words[i].charAt(0);
  152. char lastLetter = words[i].charAt( words[i].length() - 1 );
  153.  
  154. String bareWord = words[i].toLowerCase(); // site bukvi gi pravi mali
  155. if( !Character.isLetter( lastLetter ) ){ // ako ne zavrsuva so bukva
  156. bareWord = bareWord.replace( lastLetter, ' ' ); // ako zavrsuva so interpunkciski go menuva so prazno mesto
  157. }
  158.  
  159. bareWord = bareWord.trim(); // go trga praznoto mesto
  160. // vo words[i] ima zbor so site mali bukvi i bez interpunkciski znaci na krajot
  161. //System.out.println( bareWord + " " + i );
  162.  
  163. SLLNode< MapEntry< String, String > > node = table.search( bareWord );
  164.  
  165. if( node == null )
  166. continue;
  167.  
  168. String newWord = node.element.value; // go dobiva noviot zbor, treba da se sredi so interounkciski i golemi mesta
  169.  
  170. if( Character.isUpperCase( firstLetter ) ) {// ako ptvicniot zbor imal golema bukva
  171. //System.out.println( newWord + " " + i );
  172. newWord = newWord.replace( newWord.charAt( 0 ), Character.toUpperCase( newWord.charAt( 0 ) ) );
  173. }
  174.  
  175. if( !Character.isAlphabetic( lastLetter ) ){ // ako imal interpunkciski znak na kraj
  176. // System.out.println( newWord + " " + i );
  177. newWord += Character.toString( lastLetter );
  178. }
  179.  
  180. words[i] = newWord;
  181. }
  182.  
  183. tekst = "";
  184. for ( int i = 0; i < words.length; i++ ){
  185. tekst += words[i] + " ";
  186. }
  187.  
  188. System.out.println( tekst );
  189. return;
  190.  
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement