Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;import java.util.Map;
- class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
- K key;
- E value;
- public MapEntry (K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo (K that) {
- @SuppressWarnings("unchecked")
- MapEntry<K,E> other = (MapEntry<K,E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString () {
- return "(" + key + "," + value + ")";
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- private SLLNode<MapEntry<K,E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K,E>> search(K targetKey) {
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
- return curr;
- }
- return null;
- }
- public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- curr.element = newEntry;
- return;
- }
- }
- buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- int b = hash(key);
- for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(((MapEntry<K,E>) curr.element).key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.element.toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- }
- public class DedoMrazPomosnici {
- public static String srediZbor(String word)
- {
- StringBuilder sb = new StringBuilder();
- for(int i = 0 ; i < word.length() ; i++)
- {
- if(word.charAt(i)=='S' || word.charAt(i)=='s')
- {
- sb.append(word.charAt(i));
- if(word.charAt(++i)=='h')
- continue;
- }
- if(word.charAt(i)=='Z' || word.charAt(i)=='z')
- {
- sb.append(word.charAt(i));
- if(word.charAt(++i)=='h')
- continue;
- }
- if(word.charAt(i)=='C' || word.charAt(i)=='c')
- {
- sb.append(word.charAt(i));
- if(word.charAt(++i)=='h')
- continue;
- }
- sb.append(word.charAt(i));
- }
- return sb.toString();
- }
- public static void main(String[] args) throws NumberFormatException, IOException
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- CBHT<String,String>dobriDeca = new CBHT<String,String>(N*2);
- String[]imeDobriDeca = new String[N];
- String[]poklonDobriDeca = new String[N];
- String pom;
- for(int i = 0 ; i < N; i++)
- {
- pom = br.readLine();
- String[]temp = pom.split(" ");
- imeDobriDeca[i]=temp[0];
- poklonDobriDeca[i]=temp[1];
- }
- String deteZaProverka = br.readLine();
- for(int i = 0 ; i < N ; i++)
- {
- dobriDeca.insert(imeDobriDeca[i],poklonDobriDeca[i]);
- }
- SLLNode<MapEntry<String,String>> node = dobriDeca.search(srediZbor(deteZaProverka));
- if(node == null)
- System.out.println("Nema podarok");
- String podarok = node.element.value;
- System.out.println(podarok);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment