Advertisement
Guest User

prva

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