Kame3

Лозинки lab6.1

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