SashkoKlincharov

[Java][АПС] - Лозинки

Jan 23rd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. Лозинки Problem 1 (2 / 14)
  2. Потребно е да се симулира најава на еден систем. Притоа корисникот внесува корисничко име и лозинка. Доколку корисничкото име одговара со лозинката тогаш се печати Najaven, доколку не одговара се печати Nenajaven и на корисникот му се дава повторна шанса на корисникот да внесе корисничко име и лозинка. Во моментот кога корисникот ќе биде најавен престануваат обидите за најава.
  3.  
  4. Влез: Прво се дава број N на кориснички имиња и лозинки кои ќе бидат внесени во системот. Во наредните N реда се дадени корисничките имиња и лозинки разделени со едно празно место. Потоа се даваат редови со кориснички имиња и лозинки на корисници кои се обидувата да се најават (Пр. ana banana) За означување на крај на обидите во редицата се дава зборот KRAJ
  5.  
  6. Излез: За секој од влезовите кои се обид за најава се печати Nenajaven се додека не дoбиеме Najaven или додека имаме обиди за најава.
  7.  
  8. Пример. Влез: 3 ana banana pero zdero trpe trpi ana ana ana banana trpe trpi KRAJ
  9.  
  10. Излез: Nenajaven Najaven
  11.  
  12. Забелешка: Работете со хеш табела со затворени кофички. Самите решавате за големината на хеш табела, а хеш функцијата ви е дадена.
  13.  
  14. Име на класа: Lozinki
  15.  
  16.  
  17. Sample input
  18. 3
  19. ana banana
  20. pero zdero
  21. trpe trpi
  22. ana ana
  23. ana bannana
  24. trpe trpe
  25. KRAJ
  26.  
  27. Sample output
  28. Nenajaven
  29. Nenajaven
  30. Nenajaven
  31.  
  32.  
  33.  
  34. import java.io.BufferedReader;
  35. import java.io.IOException;
  36. import java.io.InputStreamReader;
  37. import java.util.Scanner;
  38.  
  39. class SLLNode<E> {
  40. protected E element;
  41. protected SLLNode<E> succ;
  42.  
  43. public SLLNode(E elem, SLLNode<E> succ) {
  44. this.element = elem;
  45. this.succ = succ;
  46. }
  47.  
  48. @Override
  49. public String toString() {
  50. return element.toString();
  51. }
  52. }
  53.  
  54. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  55.  
  56. // Each MapEntry object is a pair consisting of a key (a Comparable
  57. // object) and a value (an arbitrary object).
  58. K key;
  59. E value;
  60.  
  61. public MapEntry (K key, E val) {
  62. this.key = key;
  63. this.value = val;
  64. }
  65.  
  66. public int compareTo (K that) {
  67. // Compare this map entry to that map entry.
  68. @SuppressWarnings("unchecked")
  69. MapEntry<K,E> other = (MapEntry<K,E>) that;
  70. return this.key.compareTo(other.key);
  71. }
  72.  
  73. public String toString () {
  74. return "<" + key + "," + value + ">";
  75. }
  76. }
  77.  
  78.  
  79. class CBHT<K extends Comparable<K>, E> {
  80.  
  81. // An object of class CBHT is a closed-bucket hash table, containing
  82. // entries of class MapEntry.
  83. private SLLNode<MapEntry<K,E>>[] buckets;
  84.  
  85. @SuppressWarnings("unchecked")
  86. public CBHT(int m) {
  87. // Construct an empty CBHT with m buckets.
  88. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  89. }
  90.  
  91. private int hash(K key) {
  92. // Translate key to an index of the array buckets.
  93. return Math.abs(key.hashCode()) % buckets.length;
  94. }
  95.  
  96. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  97. // Find which if any node of this CBHT contains an entry whose key is
  98. // equal
  99. // to targetKey. Return a link to that node (or null if there is none).
  100. int b = hash(targetKey);
  101. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  102. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  103. return curr;
  104. }
  105. return null;
  106. }
  107.  
  108. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  109. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  110. int b = hash(key);
  111. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  112. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  113. // Make newEntry replace the existing entry ...
  114. curr.element = newEntry;
  115. return;
  116. }
  117. }
  118. // Insert newEntry at the front of the 1WLL in bucket b ...
  119. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  120. }
  121.  
  122. public void delete(K key) {
  123. // Delete the entry (if any) whose key is equal to key from this CBHT.
  124. int b = hash(key);
  125. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  126. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  127. if (pred == null)
  128. buckets[b] = curr.succ;
  129. else
  130. pred.succ = curr.succ;
  131. return;
  132. }
  133. }
  134. }
  135.  
  136. public String toString() {
  137. String temp = "";
  138. for (int i = 0; i < buckets.length; i++) {
  139. temp += i + ":";
  140. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  141. temp += curr.element.toString() + " ";
  142. }
  143. temp += "\n";
  144. }
  145. return temp;
  146. }
  147.  
  148. }
  149.  
  150.  
  151. public class Lozinki {
  152.  
  153. public static void main(String[] args) throws IOException {
  154. Scanner input = new Scanner(System.in);
  155. int n = input.nextInt();
  156. CBHT<String,String> hashtable = new CBHT<String,String>(n*2);
  157. for(int i=0;i<n;i++) {
  158. hashtable.insert(input.next(), input.next());
  159. }
  160. String str = input.nextLine();
  161. // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //може и со Scanner :)
  162. while(true) {
  163. str = input.nextLine();
  164. System.out.println(str);
  165. if(str.equals("KRAJ")) {
  166. break;
  167. }
  168. String [] splitter = str.split(" ");
  169. SLLNode<MapEntry<String,String>> node = hashtable.search(splitter[0]);
  170. if(node!=null&&node.element.value.equals(splitter[1])) {
  171. System.out.println("Najaven");
  172. break;
  173. }
  174. else {
  175. System.out.println("Nenajaven");
  176. }
  177.  
  178. }
  179.  
  180. }
  181.  
  182. }
Add Comment
Please, Sign In to add comment