Advertisement
Guest User

Untitled

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