Advertisement
Guest User

Untitled

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