Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Помошниците на Дедо Мраз Задача 1 (0 / 0)
- Помошниците на Дедо Мраз направиле компјутерски систем во кој се чуваа список на сите добри деца и нивната желба за подарок за Нова Година,
- само што за македонските деца употребиле стара транскрипција на специфичните македонски букви, па така буквата ч ја чуваат како c,
- буквата ж како z и ш како s. Но, кога треба да проверат дали некое дете било добро, неговото име го добиваат според новата транскрипција
- каде буквата ч се преставува како ch, буквата ж како zh и ш како sh. Помогнете им на помошниците на Дедо Мраз да проверат дали детете било добро ,
- и доколку било, кој подарок треба да го добие.
- Влез: Во првата линија е даден број N на деца кои биле добри. Во наредните N линии се дадени името на детете и поклонот кој го сака.
- Во последниот ред е дадено името на детете кое треба да се провери.
- Излез: Ако даденото дете не било добро (т.е. го нема во списокот на добри деца) да се испечати Nema poklon, а ако било добро да се испечати кој подарок го сакало.
- Име на класа: DedoMrazPomoshnici
- Делумно решение: Задачата се смета за делумно решена доколку се поминати 7 тест примери.
- Забелешка: При реализација на задачите МОРА да се користат дадените структури, а не да користат помошни структури како низи или сл.
- import java.util.Scanner;
- class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable
- // object) and a value (an arbitrary object).
- K key;
- E value;
- public MapEntry (K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo (K that) {
- // Compare this map entry to that map entry.
- @SuppressWarnings("unchecked")
- MapEntry<K,E> other = (MapEntry<K,E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString () {
- return "<" + key + "," + value + ">";
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- private SLLNode<MapEntry<K,E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K,E>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- 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)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this CBHT.
- 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;
- }
- }
- 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();
- }
- }
- public class DedoMrazPomosnici {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- input.nextLine();
- CBHT<String,String> hashtable = new CBHT<String,String>(n*2);
- for(int i=0;i<n;i++) {
- String [] string = input.nextLine().split(" ");
- String dobrodete = string[0].toLowerCase();
- String podarok = string[1];
- hashtable.insert(dobrodete, podarok);
- }
- while(true) {
- String detekoesebara = input.nextLine().toLowerCase();
- String string = "";
- if(detekoesebara.equals("kraj")) {
- break;
- }else if(detekoesebara.equals("")) {
- continue;
- }
- if(detekoesebara.contains("ch")) {
- char [] charray = detekoesebara.toCharArray();
- for(int i=0;i<charray.length;i++) {
- if(charray[i]=='c'&&charray[i+1]=='h'&&i+1<charray.length) {
- string+=(charray[i]);
- i++;
- continue;
- }
- else {
- string+=(charray[i]);
- }
- }
- detekoesebara = string;
- }
- string = "";
- if(detekoesebara.contains("sh")) {
- char [] sharray = detekoesebara.toCharArray();
- for(int i=0;i<sharray.length;i++) {
- if(sharray[i]=='s'&&sharray[i+1]=='h'&&i+1<sharray.length) {
- string+=(sharray[i]);
- i++;
- continue;
- }
- else {
- string+=(sharray[i]);
- }
- }
- detekoesebara = string;
- }
- string = "";
- if(detekoesebara.contains("zh")) {
- char [] zharray = detekoesebara.toCharArray();
- for(int i=0;i<zharray.length;i++) {
- if(zharray[i]=='z'&&zharray[i+1]=='h'&&i+1<zharray.length) {
- string+=(zharray[i]);
- i++;
- continue;
- }
- else {
- string+=(zharray[i]);
- }
- }
- detekoesebara = string;
- }
- System.out.println(detekoesebara);
- SLLNode<MapEntry<String,String>> node = hashtable.search(detekoesebara);
- if(node==null) {
- System.out.println("Nema poklon");
- }
- else {
- System.out.println(node.element.value);
- }
- }
- }
- }
- Пример влез
- 5
- JohnDoe dog
- JaneDoe cat
- TomceZarkovski bike
- MartaMartevska sonyplaystation
- EstebanPerez brother
- TomcheZharkovski
- Пример излез
- bike
Add Comment
Please, Sign In to add comment