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.*;
- 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.toString() + "," + value.toString() + ">";
- }
- }
- 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> {
- // 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) {
- // Napishete ja vie HASH FUNKCIJATA
- 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 E searchValue(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.element.value;
- }
- 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() {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < buckets.length; i++) {
- sb.append(i).append(":");
- for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- sb
- .append(curr.element.key)
- .append(" -> ")
- .append(curr.element.value)
- .append(" ");
- }
- sb.append("\n");
- }
- return sb.toString();
- }
- }
- public class KumanovskiDijalekt {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- int N = Integer.parseInt(br.readLine());
- String rechnik[] = new String[N];
- for (int i = 0; i < N; i++) {
- rechnik[i] = br.readLine();
- }
- String tekst = br.readLine();
- if (N == 0){
- System.out.println(tekst);
- return;
- }
- // Kumanovo -> Macedonian
- CBHT<String, String> cbht = new CBHT<>(2*N);
- for (int i = 0; i < rechnik.length; i++) {
- String split[] = rechnik[i].split("\\s+");
- cbht.insert(split[0], split[1]);
- }
- //System.out.println(cbht.toString());
- StringBuffer sb = new StringBuffer();
- Scanner scanner = new Scanner(tekst);
- while (scanner.hasNext()) {
- String word = scanner.next();
- String translation = cbht.searchValue(trim(word).toLowerCase());
- if (translation == null) {
- sb.append(word).append(" ");
- } else {
- Character last = word.charAt(word.length() - 1);
- if (!Character.isAlphabetic(last)){
- translation = translation + last;
- }
- if (Character.isUpperCase(word.charAt(0))){
- translation = capitalize(translation);
- }
- sb.append(translation).append(" ");
- }
- }
- System.out.println(sb.toString().trim());
- }
- public static String trim(String str) {
- char last = str.charAt(str.length() - 1);
- if (!Character.isAlphabetic(last))
- return str.substring(0, str.length() - 1);
- return str;
- }
- public static String capitalize(String word) {
- if (Character.isUpperCase(word.charAt(0)))
- return word; // word is already capitalized
- return word.substring(0, 1).toUpperCase() + word.substring(1);
- }
- }
- /**
- * You are given a dictionary of words in the kumanovo dialect, and how they are written in the macedonian language. Then, you are given a text written in the kumanovo dialect. You need to swap all occurences of a dialect word with the corresponding standard macedonian word.
- * <p>
- * Note: You should avoid the punctuation marks, dot (.) , comma (,), exclamation point (!) and question mark (?). The swapped words should keep the case they were originally written in (case sensitive).
- */
Advertisement
Add Comment
Please, Sign In to add comment