kristina7

Лозинки

Jan 17th, 2017
199
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.  
  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.  
  22.     // Each MapEntry object is a pair consisting of a key (a Comparable
  23.     // object) and a value (an arbitrary object).
  24.     K key;
  25.     E value;
  26.  
  27.     public MapEntry (K key, E val) {
  28.         this.key = key;
  29.         this.value = val;
  30.     }
  31.    
  32.     public int compareTo (K that) {
  33.     // Compare this map entry to that map entry.
  34.         @SuppressWarnings("unchecked")
  35.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  36.         return this.key.compareTo(other.key);
  37.     }
  38.  
  39.     public String toString () {
  40.         return "<" + key + "," + value + ">";
  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.         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.                 return;
  96.             }
  97.         }
  98.     }
  99.  
  100.     public String toString() {
  101.         String temp = "";
  102.         for (int i = 0; i < buckets.length; i++) {
  103.             temp += i + ":";
  104.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  105.                 temp += curr.element.toString() + " ";
  106.             }
  107.             temp += "\n";
  108.         }
  109.         return temp;
  110.     }
  111.  
  112. }
  113.  
  114. public class Lozinki {
  115.  
  116.     public static void main(String[] args) throws IOException{
  117.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  118.         int N = Integer.parseInt(br.readLine());
  119.         CBHT<String, String> table = new CBHT<>(N/2+1);
  120.         for(int i=0;i<N;i++){
  121.             String str = br.readLine();
  122.             String[] s = str.split(" ");
  123.             table.insert(s[0], s[1]);
  124.         }
  125.         while(true){
  126.             String zbor = br.readLine();
  127.             if(zbor.equals("KRAJ"))
  128.                 break;
  129.             String[] z = zbor.split(" ");
  130.             SLLNode<MapEntry<String, String>> node = table.search(z[0]);
  131.            
  132.             if(node != null)
  133.             {
  134.                 //System.out.println( node.element.value.length()+"/" + z[1].length());
  135.                
  136.                 if(node.element.value.equals(z[1])){
  137.                     System.out.println("Najaven");
  138.                     break;
  139.                 }                  
  140.                 else
  141.                     System.out.println("Nenajaven");
  142.             }else
  143.                 System.out.println("Nenajaven");
  144.                
  145.             /*3
  146. ana banana
  147. pero zdero
  148. trpe trpi
  149. ana ana
  150. ana bannana
  151. trpe trpe
  152. KRAJ*/
  153.         }
  154.        
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment