SashkoKlincharov

[Java][АПС] - На граница

Feb 6th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class CBHT<K extends Comparable<K>, E> {
  5.  
  6. // An object of class CBHT is a closed-bucket hash table, containing
  7. // entries of class MapEntry.
  8. private SLLNode<MapEntry<K,E>>[] buckets;
  9.  
  10. @SuppressWarnings("unchecked")
  11. public CBHT(int m) {
  12. // Construct an empty CBHT with m buckets.
  13. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  14. }
  15.  
  16. private int hash(K key) {
  17. // Translate key to an index of the array buckets.
  18. return Math.abs(key.hashCode()) % buckets.length;
  19. }
  20.  
  21. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  22. // Find which if any node of this CBHT contains an entry whose key is
  23. // equal
  24. // to targetKey. Return a link to that node (or null if there is none).
  25. int b = hash(targetKey);
  26. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  27. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  28. return curr;
  29. }
  30. return null;
  31. }
  32.  
  33. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  34. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  35. int b = hash(key);
  36. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  37. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  38. // Make newEntry replace the existing entry ...
  39. curr.element = newEntry;
  40. return;
  41. }
  42. }
  43. // Insert newEntry at the front of the 1WLL in bucket b ...
  44. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  45. }
  46.  
  47. public void delete(K key) {
  48. // Delete the entry (if any) whose key is equal to key from this CBHT.
  49. int b = hash(key);
  50. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  51. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  52. if (pred == null)
  53. buckets[b] = curr.succ;
  54. else
  55. pred.succ = curr.succ;
  56. return;
  57. }
  58. }
  59. }
  60.  
  61. public String toString() {
  62. String temp = "";
  63. for (int i = 0; i < buckets.length; i++) {
  64. temp += i + ":";
  65. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  66. temp += curr.element.toString() + " ";
  67. }
  68. temp += "\n";
  69. }
  70. return temp;
  71. }
  72.  
  73. }
  74. class SLLNode<E> {
  75. protected E element;
  76. protected SLLNode<E> succ;
  77.  
  78. public SLLNode(E elem, SLLNode<E> succ) {
  79. this.element = elem;
  80. this.succ = succ;
  81. }
  82.  
  83. @Override
  84. public String toString() {
  85. return element.toString();
  86. }
  87. }
  88. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  89.  
  90. // Each MapEntry object is a pair consisting of a key (a Comparable
  91. // object) and a value (an arbitrary object).
  92. K key;
  93. E value;
  94.  
  95. public MapEntry (K key, E val) {
  96. this.key = key;
  97. this.value = val;
  98. }
  99.  
  100. public int compareTo (K that) {
  101. // Compare this map entry to that map entry.
  102. @SuppressWarnings("unchecked")
  103. MapEntry<K,E> other = (MapEntry<K,E>) that;
  104. return this.key.compareTo(other.key);
  105. }
  106.  
  107. public String toString () {
  108. return "<" + key + "," + value + ">";
  109. }
  110. }
  111. /*
  112.  
  113. 4
  114. A111111 Katerina
  115. B111111 Ilinka
  116. B222222 Elena
  117. C333333 Frosina
  118. 2
  119. Katerina Kejt
  120. Elena Helena
  121. A111111
  122. */
  123. public class NaGranica {
  124.  
  125. public static void main(String[] args) {
  126. Scanner input = new Scanner(System.in);
  127. int n = input.nextInt();
  128. input.nextLine();
  129.  
  130. CBHT<String,String> hashtable = new CBHT<String,String>(n*2);
  131. for(int i=0;i<n;i++) {
  132. String [] read = input.nextLine().split(" ");
  133. String brpasos = read[0];
  134. String ime = read[1];
  135. hashtable.insert(brpasos, ime);
  136. }
  137. int m = input.nextInt();
  138. input.nextLine();
  139. CBHT<String,String> ht2 = new CBHT<String,String>(m*2);
  140. for(int i=0;i<m;i++) {
  141. String [] read = input.nextLine().split(" ");
  142. ht2.insert(read[0], read[1]);
  143. }
  144. String sebara = input.nextLine();
  145. SLLNode<MapEntry<String,String>> node = hashtable.search(sebara);
  146. if(node!=null) {
  147. if(ht2.search(node.element.value)!=null) {
  148. System.out.println("Ne smee");
  149. }
  150. else {
  151. System.out.println("Smee");
  152. }
  153. }
  154. else {
  155. System.out.println("Ne smee");
  156. }
  157. }
  158.  
  159. }
Add Comment
Please, Sign In to add comment