Advertisement
Guest User

Untitled

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