Mitko_jos

za nomce :)

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