Advertisement
Guest User

Untitled

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