196040

APS labs 6 Lozinki

Dec 14th, 2020 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class SLLNode<E> {
  5.     protected E element;
  6.     protected SLLNode<E> succ;
  7.     public SLLNode(E elem, SLLNode<E> succ) {
  8.         this.element = elem;
  9.         this.succ = succ;
  10.     }
  11.  
  12.     @Override
  13.     public String toString() {
  14.         return element.toString();
  15.     }
  16. }
  17. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  18.     K key;    // Each MapEntry object is a pair consisting of a key (a Comparable
  19.     E value;    // object) and a value (an arbitrary object).
  20.     public MapEntry (K key, E val) {
  21.         this.key = key;
  22.         this.value = val;
  23.     }
  24.     public int compareTo (K that) { // Compare this map entry to that map entry.
  25.         @SuppressWarnings("unchecked")
  26.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  27.         return this.key.compareTo(other.key);
  28.     }
  29.     public String toString () {
  30.         return "<" + key + "," + value + ">";
  31.     }
  32. }
  33. class CBHT<K extends Comparable<K>, E> { // An object of class CBHT is a closed-bucket hash table, containing
  34.     // entries of class MapEntry.
  35.     private SLLNode<MapEntry<K,E>>[] buckets;
  36.     @SuppressWarnings("unchecked")
  37.     public CBHT(int m) { // Construct an empty CBHT with m buckets.
  38.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  39.     }
  40.     private int hash(K key) { // Translate key to an index of the array buckets.
  41.         return Math.abs(key.hashCode()) % buckets.length;
  42.     }
  43.     public SLLNode<MapEntry<K,E>> search(K targetKey) { // Find which if any node of this CBHT contains an entry whose key is
  44.         // equal to targetKey. Return a link to that node (or null if there is none).
  45.         int b = hash(targetKey);
  46.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  47.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  48.                 return curr;
  49.         }
  50.         return null;
  51.     }
  52.     public void insert(K key, E val) {  // Insert the entry <key, val> into this CBHT.
  53.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  54.         int b = hash(key);
  55.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  56.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  57.                 // Make newEntry replace the existing entry ...
  58.                 curr.element = newEntry;
  59.                 return;
  60.             }
  61.         } // Insert newEntry at the front of the 1WLL in bucket b ...
  62.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  63.     }
  64.     public void delete(K key) { // 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.     public String toString() {
  77.         String temp = "";
  78.         for (int i = 0; i < buckets.length; i++) {
  79.             temp += i + ":";
  80.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  81.                 temp += curr.element.toString() + " ";
  82.             }
  83.             temp += "\n";
  84.         }
  85.         return temp;
  86.     }
  87.     public void check(K key, E val) {
  88.         if(key.equals(val))
  89.             System.out.println("Najaven");
  90.         else System.out.println("Nenajaven");
  91.     }
  92. }
  93. public class Lozinki {
  94.     public static void main (String[] args) throws IOException {
  95.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  96.         int N = Integer.parseInt(br.readLine());
  97.         CBHT<String, String> kofa = new CBHT<String, String>(26);
  98.         for(int i=1; i<=N; i++) {
  99.             String imelozinka = br.readLine();
  100.             String[] pom = imelozinka.split(" ");
  101.             kofa.insert(pom[0], pom[1]);
  102.         }
  103.         for(;;) {
  104.             String check = br.readLine();
  105.             String[] help = check.split(" ");
  106.             if(help[0].equals("KRAJ"))
  107.                 break;
  108.             if(kofa.search(help[0])!=null) {
  109.                 if(help[1].equals(kofa.search(help[0]).element.value)) {
  110.                     System.out.println("Najaven");
  111.                     break;
  112.                 } else System.out.println("Nenajaven");
  113.             } else System.out.println("Nenajaven");
  114.         }
  115.     }
  116. }/*
  117. Потребно е да се симулира најава на еден систем. Притоа корисникот внесува корисничко име и лозинка.
  118. Доколку корисничкото име одговара со лозинката тогаш се печати Najaven, доколку не одговара се печати Nenajaven
  119. и на корисникот му се дава повторна шанса на корисникот да внесе корисничко име и лозинка.
  120. Во моментот кога корисникот ќе биде најавен престануваат обидите за најава.
  121. Влез: Прво се дава број N на кориснички имиња и лозинки кои ќе бидат внесени во системот.
  122. Во наредните N реда се дадени корисничките имиња и лозинки разделени со едно празно место.
  123. Потоа се даваат редови со кориснички имиња и лозинки на корисници кои се обидувата да се најават (Пр. ana banana)
  124. За означување на крај на обидите во редицата се дава зборот KRAJ.
  125. Излез: За секој од влезовите кои се обид за најава се печати Nenajaven се додека не дoбиеме Najave
  126. n или додека имаме обиди за најава.
  127. Пример. Влез: 3 ana banana pero zdero trpe trpi ana ana ana banana trpe trpi KRAJ
  128. Излез: Nenajaven Najaven
  129. Забелешка: Работете со хеш табела со затворени кофички. Самите решавате за големината на хеш табела,
  130. а хеш функцијата ви е дадена.
  131. Име на класа: Lozinki
  132. */
Add Comment
Please, Sign In to add comment