Advertisement
Guest User

Untitled

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