Crazy

Лозинки

Nov 25th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5.  
  6.  
  7. class CBHT<K extends Comparable<K>,E> {
  8.  
  9.     private SLLNode<MapEntry<K,E>>[] buckets;
  10.  
  11.     @SuppressWarnings("unchecked")
  12.     public CBHT(int m) {
  13.  
  14.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  15.     }
  16.  
  17.     private int hash(K key) {
  18.         // Translate key to an index of the array buckets.
  19.         return Math.abs(key.hashCode()) % buckets.length;
  20.     }
  21.  
  22.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  23.         // Find which if any node of this CBHT contains an entry whose key is
  24.         // equal
  25.         // to targetKey. Return a link to that node (or null if there is none).
  26.         int b = hash(targetKey);
  27.         SLLNode<MapEntry<K,E>> curr = buckets[b];
  28.         for (; curr != null; curr = curr.succ) {
  29.             if (targetKey.equals(curr.getElement().key)) {
  30.                 return curr;
  31.             }
  32.         }
  33.         return null;
  34.  
  35.     }
  36.  
  37.     public void insert(K key, E value) {
  38.  
  39.         MapEntry<K,E> newEntry = new MapEntry<K,E>(key, value);
  40.         int b = hash(key);
  41.         SLLNode<MapEntry<K,E>> curr = buckets[b];
  42.         for (; curr != null; curr = curr.succ) {
  43.             if (key.equals(curr.getElement().key)) {
  44.                 curr.setElement(newEntry);
  45.                 return;
  46.             }
  47.         }
  48.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  49.     }
  50.  
  51.     public void delete (K key) {
  52.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  53.         int b = hash(key);
  54.         SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b];
  55.         for (; curr != null; pred = curr, curr = curr.succ) {
  56.             if (key.equals(curr.getElement().key)) {
  57.                 if (pred == null)
  58.                     buckets[b] = curr.succ;
  59.                 else
  60.                     pred.succ = curr.succ;
  61.                 return;
  62.             }
  63.         }
  64.     }
  65.  
  66.     public String toString() {
  67.         String temp = "";
  68.         for (int i = 0; i < buckets.length; i++) {
  69.             temp += i + ":";
  70.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  71.                 temp += curr.getElement().toString() + " ";
  72.             }
  73.             temp += "\n";
  74.         }
  75.         return temp;
  76.     }
  77.  
  78. }
  79.  
  80. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  81.  
  82.     K key;
  83.     E value;
  84.  
  85.     public MapEntry(K key, E value) {
  86.         this.key = key;
  87.         this.value = value;
  88.     }
  89.  
  90.     @Override
  91.     public int compareTo(K that) {
  92.         return this.key.compareTo(that);
  93.     }
  94.  
  95.     @Override
  96.     public String toString () {
  97.         return "<" + key + "," + value + ">";
  98.     }
  99. }
  100.  
  101. class SLLNode<E> {
  102.  
  103.     private E element;
  104.     public SLLNode<E> succ;
  105.  
  106.     public SLLNode(E element, SLLNode<E> succ) {
  107.         this.setElement(element);
  108.         this.succ = succ;
  109.     }
  110.  
  111.     @Override
  112.     public String toString() {
  113.         return getElement().toString();
  114.     }
  115.  
  116.     public E getElement() {
  117.         return element;
  118.     }
  119.  
  120.     public void setElement(E element) {
  121.         this.element = element;
  122.     }
  123.  
  124. }
  125.  
  126. public class Lozinki {
  127.     public static void main (String[] args) throws IOException {
  128.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  129.         int N = Integer.parseInt(br.readLine());
  130.  
  131.         HashMap<String, String> tabela = new HashMap<>();
  132.  
  133.         for(int i=1;i<=N;i++){
  134.             String imelozinka = br.readLine();
  135.             String[] pom = imelozinka.split(" ");
  136.             tabela.put(pom[0], pom[1]);
  137.  
  138.         }
  139.  
  140.         String s = br.readLine();
  141.  
  142.         while (!s.equals("KRAJ"))
  143.         {
  144.             String [] pom = s.split(" ");
  145.             if (tabela.containsKey(pom[0]) && tabela.get(pom[0]).equals(pom[1]))
  146.             {
  147.                 System.out.println("Najaven");
  148.                 break;
  149.             }
  150.             else
  151.                 System.out.println("Nenajaven");
  152.  
  153.             s=br.readLine();
  154.  
  155.         }
  156.  
  157.  
  158.  
  159.  
  160.  
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment