Last active
March 22, 2018 23:23
-
-
Save ignaciobll/4373bc0a587223b84a86cf0707b1e80e to your computer and use it in GitHub Desktop.
Ada Byron Fase Local UPM - Problema 865 - Sustitution Cipher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/ | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.util.Scanner; | |
| import java.util.StringTokenizer; | |
| import java.util.HashMap; | |
| // Imports solo para leer decentemente. | |
| // Por algo amamos Java para aprender a programar (no). | |
| class Main { | |
| static class FastReader { | |
| BufferedReader br; | |
| StringTokenizer st; | |
| public FastReader() { | |
| br = new BufferedReader(new InputStreamReader(System.in)); | |
| } | |
| String next(){ | |
| while (st == null || !st.hasMoreElements()){ | |
| try { | |
| st = new StringTokenizer(br.readLine()); | |
| } catch (IOException e) { | |
| System.out.println("Error " + e.getMessage()); | |
| } | |
| } | |
| return st.nextToken(); | |
| } | |
| int nextInt(){ | |
| return Integer.parseInt(next()); | |
| } | |
| long nextLong(){ | |
| return Long.parseLong(next()); | |
| } | |
| double nextDouble(){ | |
| return Double.parseDouble(next()); | |
| } | |
| String nextLine(){ | |
| String str = ""; | |
| try { | |
| str = br.readLine(); | |
| } | |
| catch (IOException e) { | |
| return null; | |
| } | |
| return str; | |
| } | |
| } | |
| public static void main (String [] args) { | |
| FastReader fr = new FastReader(); | |
| int n = fr.nextInt(); | |
| // System.out.println("Número de casos: " + n); | |
| fr.nextLine(); // Empty line | |
| for (int i = 0; i<n; i++){ // For cases | |
| // System.out.println("Caso " + i); | |
| if (i > 0) | |
| System.out.println(""); | |
| String alfa = fr.nextLine(); // Se lee el alfabeto | |
| String sust = fr.nextLine(); // Se lee la sustitución | |
| System.out.println(sust); | |
| System.out.println(alfa); | |
| int len = alfa.length(); | |
| // System.out.println("Len: " + len); | |
| HashMap<Character, Character> cipher = new HashMap<Character, Character>(len); // Estructura para el cambio (K->V) | |
| for (int j = 0; j<len; j++){ | |
| // System.out.println("J: " + j + " Alfa: " + alfa.charAt(j) + " Sust: " + sust.charAt(j)); | |
| cipher.put((Character) alfa.charAt(j), (Character) sust.charAt(j)); | |
| } | |
| // System.out.println("HashMap size: " + cipher.size()); | |
| // System.out.println("HashMap keys: " + cipher.get('c')); | |
| String line = fr.nextLine(); | |
| while (line != null && !line.equals("")){ | |
| for (Character c : line.toCharArray()) | |
| System.out.print(cipher.getOrDefault(c, c)); | |
| System.out.println(""); | |
| line = fr.nextLine(); | |
| // System.out.println("Linea: " + line); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment