(UVA) Babelfish - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1223

A map structure is used to keep a foreign language as the key and its translation in English as the value. Then, when the words in the foreign language are read, it is only necessary to associate them with the values on the map.


import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        HashMap<String, String> dictionary = new HashMap<>();
        
        String line = br.readLine();
        while (!line.equals("")) {
            String[] s = line.split("\\s");
            dictionary.put(s[1], s[0]);
            
            line = br.readLine();
        }
        
        line = br.readLine();
        while (line != null) {
            if (!dictionary.containsKey(line)) {
                System.out.println("eh");
            }
            else {
                System.out.println(dictionary.get(line));
            }
            
            line = br.readLine();
        }
                       
        return;
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
        
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução