Filip_Markoski

[ADS] Passwords

Nov 22nd, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  5.  
  6. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  7.  
  8.     K key;
  9.     E value;
  10.  
  11.     public MapEntry(K key, E val) {
  12.         this.key = key;
  13.         this.value = val;
  14.     }
  15.  
  16.     public int compareTo(K that) {
  17.         @SuppressWarnings("unchecked")
  18.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  19.         return this.key.compareTo(other.key);
  20.     }
  21.  
  22.     public String toString() {
  23.         return "(" + key + "," + value + ")";
  24.     }
  25. }
  26.  
  27. class SLLNode<E> {
  28.     protected E element;
  29.     protected SLLNode<E> succ;
  30.  
  31.     public SLLNode(E elem, SLLNode<E> succ) {
  32.         this.element = elem;
  33.         this.succ = succ;
  34.     }
  35.  
  36.     @Override
  37.     public String toString() {
  38.         return element.toString();
  39.     }
  40. }
  41.  
  42. class CBHT<K extends Comparable<K>, E> {
  43.  
  44.     private SLLNode<MapEntry<K, E>>[] buckets;
  45.  
  46.     @SuppressWarnings("unchecked")
  47.     public CBHT(int m) {
  48.         buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
  49.     }
  50.  
  51.     private int hash(K key) {
  52.         return Math.abs(key.hashCode()) % buckets.length;
  53.     }
  54.  
  55.     public SLLNode<MapEntry<K, E>> search(K targetKey) {
  56.         int b = hash(targetKey);
  57.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  58.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  59.                 return curr;
  60.         }
  61.         return null;
  62.     }
  63.  
  64.     public void insert(K key, E val) {        // Insert the entry <key, val> into this CBHT.
  65.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  66.         int b = hash(key);
  67.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  68.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  69.                 curr.element = newEntry;
  70.                 return;
  71.             }
  72.         }
  73.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  74.     }
  75.  
  76.     public void delete(K key) {
  77.         int b = hash(key);
  78.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  79.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  80.                 if (pred == null)
  81.                     buckets[b] = curr.succ;
  82.                 else
  83.                     pred.succ = curr.succ;
  84.                 return;
  85.             }
  86.         }
  87.     }
  88.  
  89.     public String toString() {
  90.         String temp = "";
  91.         for (int i = 0; i < buckets.length; i++) {
  92.             temp += i + ":";
  93.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  94.                 temp += curr.element.toString() + " ";
  95.             }
  96.             temp += "\n";
  97.         }
  98.         return temp;
  99.     }
  100.  
  101. }
  102.  
  103. public class Lozinki {
  104.     public static void main(String[] args) throws IOException {
  105.         Scanner scanner = new Scanner(System.in);
  106.  
  107.         int existInTheSystem = scanner.nextInt();
  108.         CBHT<String, String> table = new CBHT<String, String>(existInTheSystem);
  109.         StringBuffer sb = new StringBuffer();
  110.         for (int i = 0; i < existInTheSystem; i++) {
  111.             String username = scanner.next();
  112.             String password = scanner.next();
  113.  
  114.             sb.append(String.format("%s %s\n", username, password));
  115.  
  116.             table.insert(username, password);
  117.         }
  118.         /*System.out.println("Exist In the system:");
  119.         System.out.println(sb.toString());
  120.  
  121.         System.out.println("Trying to log in:");*/
  122.  
  123.         while (scanner.hasNext()) {
  124.             String username = scanner.next();
  125.             if (username.equals("KRAJ")) break;
  126.             String password = scanner.next();
  127.             //System.out.println(String.format("%s %s", username, password));
  128.  
  129.             SLLNode<MapEntry<String, String>> query = table.search(username);
  130.             //System.out.println(query);
  131.             if (query != null && query.element.value.equals(password)) {
  132.                 System.out.println("Najaven");
  133.                 break; // If you find one break out of the while
  134.             } else {
  135.                 System.out.println("Nenajaven");
  136.             }
  137.  
  138.  
  139.         }
  140.  
  141.  
  142.     }
  143. }
Add Comment
Please, Sign In to add comment