DamSi

Untitled

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