acxl

code base version 1.8

Dec 14th, 2020
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.58 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 Preveduvac {
  100.     public static void main(String [] args) throws IOException{
  101.         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  102.         int N = Integer.parseInt(input.readLine());
  103.         HashMap<String, String> hashMap = new HashMap<String, String>(131);
  104.  
  105.         for(int i=0; i<N; i++){
  106.             String words = input.readLine();
  107.             String split[] = words.split(" ");
  108.             hashMap.insert(split[1], split[0]);
  109.         }
  110.  
  111.         while(true){
  112.             String englishWord = input.readLine();
  113.             if(englishWord.equals("KRAJ"))
  114.                 break;
  115.  
  116.             if(hashMap.search(englishWord) != null)
  117.                 System.out.println(hashMap.search(englishWord).element.value);
  118.             else
  119.                 System.out.println("/");
  120.         }
  121.     }
  122. }
Add Comment
Please, Sign In to add comment