Latkoski

Дедо Мраз Адреси

Jan 9th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 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.     // Each MapEntry object is a pair consisting of a key (a Comparable
  8.     // object) and a value (an arbitrary object).
  9.     K key;
  10.     E value;
  11.  
  12.     public MapEntry (K key, E val) {
  13.         this.key = key;
  14.         this.value = val;
  15.     }
  16.    
  17.     public int compareTo (K that) {
  18.     // Compare this map entry to that map entry.
  19.         @SuppressWarnings("unchecked")
  20.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  21.         return this.key.compareTo(other.key);
  22.     }
  23.  
  24.     public String toString () {
  25.         return "<" + key + "," + value + ">";
  26.     }
  27. }
  28.  
  29. class SLLNode<E> {
  30.     protected E element;
  31.     protected SLLNode<E> succ;
  32.  
  33.     public SLLNode(E elem, SLLNode<E> succ) {
  34.         this.element = elem;
  35.         this.succ = succ;
  36.     }
  37.  
  38.     @Override
  39.     public String toString() {
  40.         return element.toString();
  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.  
  115. public class DedoMrazAdresi {
  116.  
  117.     public static void main(String[] args) throws NumberFormatException, IOException
  118.     {
  119.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  120.         int N = Integer.parseInt(br.readLine());//broj na deca koi bile dobri
  121.         CBHT<String,String> dobriDeca = new CBHT<String,String>(N*2);//*2 poradi loadFactor 0.5-0.75
  122.         String[]imeDobriDeca = new String[N];
  123.         String[]adresiDobriDeca = new String[N];
  124.         String ime,adresa;
  125.         for(int i = 0 ; i < N ; i++)
  126.         {
  127.             ime = br.readLine();
  128.             adresa = br.readLine();
  129.             imeDobriDeca[i] = ime;//vnesuvanje na imeto
  130.             adresiDobriDeca[i] = adresa;//vnesuvanje na adresata
  131.         }
  132.         int M = Integer.parseInt(br.readLine());
  133.         CBHT<String,String>promenetiUlici = new CBHT<String,String>(M*2);
  134.         String[] staro_ime = new String[M];
  135.         String[] novo_ime = new String[M];
  136.         String temp;
  137.         for(int i = 0 ; i < M ; i++)
  138.         {
  139.             temp = br.readLine();
  140.             String[]podeleni = temp.split(" ");
  141.             staro_ime[i] = podeleni[0];
  142.             novo_ime[i] = podeleni[1];
  143.         }
  144.         String deteZaProverka = br.readLine();
  145.    
  146.         for(int i = 0 ; i < N ; i++)
  147.             dobriDeca.insert(imeDobriDeca[i],adresiDobriDeca[i]);//polnime deca so podatoci
  148.         for(int i = 0 ; i < M ; i++)
  149.             promenetiUlici.insert(staro_ime[i],novo_ime[i]);//polnime iminja na ulici
  150.         SLLNode<MapEntry<String,String>>node = dobriDeca.search(deteZaProverka);//go barame deteto
  151.         if(node==null)
  152.             System.out.println("Nema podarok");//ako deteto go nemat vo hashot
  153.        
  154.         String ulica = node.element.value;
  155.         String[] ulicaSearch = ulica.split(" ");
  156.        
  157.         if(!ulicaSearch[2].equals("Skopje")){//go delime od prviot hash na 3, ime,ulica,grad za proverka
  158.             System.out.println("Drug grad");
  159.             return;
  160.         }
  161.         SLLNode<MapEntry<String,String>>node1 = promenetiUlici.search(ulicaSearch[0]);
  162.         String novaUlicaPrint = node1.element.value;
  163.         ulicaSearch[0] = novaUlicaPrint;
  164.         for(int i = 0 ; i < ulicaSearch.length;i++)
  165.             System.out.println(ulicaSearch[i] + " ");
  166.     }
  167. }
Add Comment
Please, Sign In to add comment