Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 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.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. }
  113.  
  114. public class KumanovskiDijalekt {
  115. public static void main (String[] args) throws IOException {
  116.  
  117. BufferedReader br = new BufferedReader(new InputStreamReader(
  118. System.in));
  119. int N = Integer.parseInt(br.readLine());
  120.  
  121. String rechnik[]=new String[N];
  122. for(int i=0;i<N;i++){
  123. rechnik[i]=br.readLine();
  124. }
  125.  
  126. String tekst=br.readLine();
  127.  
  128. CBHT<String, String> table = new CBHT<String,String>(2*(N+1));
  129. for(int i = 0; i < N; i++){
  130. String [] pom = rechnik[i].split(" ");
  131. table.insert(pom[0], pom[1]);
  132. }
  133. //System.out.println(table.toString());
  134. String [] words = tekst.split(" ");
  135. for(int i = 0; i < words.length; i++){
  136. String tempWord = words[i].toLowerCase();
  137. boolean upper = false, end = false;
  138. if(Character.isUpperCase(words[i].charAt(0))){
  139. upper = true;
  140. }
  141. if(!Character.isLetterOrDigit(words[i].charAt(words[i].length()-1))){
  142. end = true;
  143. tempWord = tempWord.substring(0, words[i].length()-1);
  144. }
  145. if(table.search(tempWord) != null){
  146. String dictWord;
  147. dictWord = table.search(tempWord).element.value;
  148. tempWord = dictWord;
  149. }
  150. if(end == true){
  151. tempWord = tempWord.concat(Character.toString(words[i].charAt(words[i].length()-1)));
  152. }
  153. if(upper == true){
  154. String temp = Character.toString(tempWord.charAt(0)).toUpperCase();
  155. tempWord = temp.concat(tempWord.substring(1));
  156. }
  157. System.out.print(tempWord+" ");
  158. }
  159.  
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement