DamSi

Untitled

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