Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 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. return Math.abs(key.toString().hashCode())%51;
  58. // Napishete ja vie HASH FUNKCIJATA
  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.  
  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. CBHT<String, String> cbht = new CBHT<>(51);
  123.  
  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. cbht.insert(split[0].toLowerCase(), split[1]);
  129. }
  130.  
  131. String tekst=br.readLine();
  132. String []parts = tekst.split(" ");
  133. StringBuilder sb = new StringBuilder();
  134. for (int i=0; i<parts.length; i++) {
  135. if (parts[i].endsWith(".")) {
  136. if (cbht.search(parts[i].substring(0, parts[i].length() - 1)) == null) {
  137. sb.append(parts[i] + " ");
  138. } else {
  139. sb.append(cbht.search(parts[i].substring(0, parts[i].length() - 1)).element.value + ". ");
  140. }
  141. }
  142. else if(parts[i].endsWith(",")){
  143. if (cbht.search(parts[i].substring(0, parts[i].length() - 1)) == null) {
  144. sb.append(parts[i] + " ");
  145. } else {
  146. sb.append(cbht.search(parts[i].substring(0, parts[i].length() - 1)).element.value + ", ");
  147. }
  148. }
  149. else {
  150. if (cbht.search(parts[i].toLowerCase()) == null)
  151. sb.append(parts[i] + " ");
  152. else {
  153. if (sb.toString().endsWith(". ") || sb.toString().length()==0){
  154. String a=cbht.search(parts[i].toLowerCase()).element.value;
  155. sb.append(a.substring(0 ,1).toUpperCase() + a.substring(1) + " ");
  156. }
  157. else{
  158. sb.append(cbht.search(parts[i].toLowerCase()).element.value + " ");}
  159. }
  160. }
  161. }
  162. System.out.println(sb.toString().substring(0, sb.toString().length()-1));
  163. //Vasiot kod tuka
  164.  
  165.  
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement