Advertisement
metalni

APS Labs 6 Lozinki

Dec 14th, 2020
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class SLLNode<E> {
  6.     protected E element;
  7.     protected SLLNode<E> succ;
  8.  
  9.     public SLLNode(E elem, SLLNode<E> succ) {
  10.         this.element = elem;
  11.         this.succ = succ;
  12.     }
  13.  
  14.     @Override
  15.     public String toString() {
  16.         return element.toString();
  17.     }
  18. }
  19.  
  20. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  21.     K key;
  22.     E value;
  23.  
  24.     public MapEntry (K key, E val) {
  25.         this.key = key;
  26.         this.value = val;
  27.     }
  28.  
  29.     public int compareTo (K that) {
  30.         @SuppressWarnings("unchecked")
  31.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  32.         return this.key.compareTo(other.key);
  33.     }
  34.  
  35.     public String toString () {
  36.         return "<" + key + "," + value + ">";
  37.     }
  38. }
  39.  
  40. class HashMap<K extends Comparable<K>, E> {
  41.     private SLLNode<MapEntry<K,E>>[] buckets;
  42.  
  43.     @SuppressWarnings("unchecked")
  44.     public HashMap(int m) {
  45.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  46.     }
  47.  
  48.     private int hash(K key) {
  49.         return Math.abs(key.hashCode()) % buckets.length;
  50.     }
  51.  
  52.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  53.         int b = hash(targetKey);
  54.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  55.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  56.                 return curr;
  57.         }
  58.         return null;
  59.     }
  60.  
  61.     public void insert(K key, E val) {
  62.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  63.         int b = hash(key);
  64.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  65.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  66.                 curr.element = newEntry;
  67.                 return;
  68.             }
  69.         }
  70.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  71.     }
  72.  
  73.     public void delete(K key) {
  74.         int b = hash(key);
  75.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  76.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  77.                 if (pred == null)
  78.                     buckets[b] = curr.succ;
  79.                 else
  80.                     pred.succ = curr.succ;
  81.                 return;
  82.             }
  83.         }
  84.     }
  85.  
  86.     public String toString() {
  87.         String temp = "";
  88.         for (int i = 0; i < buckets.length; i++) {
  89.             temp += i + ":";
  90.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  91.                 temp += curr.element.toString() + " ";
  92.             }
  93.             temp += "\n";
  94.         }
  95.         return temp;
  96.     }
  97. }
  98.  
  99. public class Lozinki {
  100.     public static void main (String[] args) throws IOException {
  101.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  102.         int N = Integer.parseInt(br.readLine());
  103.         HashMap<String, String> hashMap = new HashMap<String, String>(31);
  104.  
  105.         for(int i=1;i<=N;i++){
  106.             String imelozinka = br.readLine();
  107.             String[] split = imelozinka.split(" ");
  108.             hashMap.insert(split[0], split[1]);
  109.         }
  110.  
  111.         while(true) {
  112.             String imeLozinka = br.readLine();
  113.             String[] split = imeLozinka.split(" ");
  114.             if (split[0].equals("KRAJ"))
  115.                 break;
  116.             if (hashMap.search(split[0]) != null) {
  117.                 if (split[1].equals(hashMap.search(split[0]).element.value)) {
  118.                     System.out.println("Najaven");
  119.                     break;
  120.                 } else
  121.                     System.out.println("Nenajaven");
  122.             } else
  123.                 System.out.println("Nenajaven");
  124.         }
  125.  
  126.     }
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement