Key to Success |
Any one-to-one mapping, f, of any alphabet to itself can be used to encode text by replacing each occurrence of any letter, c, with f(c). One such mapping could be the mapping of a letter to three positions beyond the letter in the alphabet. That is, ,
,
,
and so on.
With this mapping, ``The car is blue'' will be encoded as ``Wkh fdu lv eoxh''.
Input and Output
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Your correct program should decodes the contents of each input set according to the following guidelines:
- Only letters are encoded. Letters are mapped to letters. Uppercase letters are different from their lowercase counter parts.
- The mapping that defines the encoding is one-to-one. That is, two different letters never map to the same letter of the alphabet (
and
is impossible).
- There are two input lines - the first one contains a text (not encoded) and the second one contains an encoded text. This text is to be decoded by your program.
- Both lines are written by the same person.
- It is to be assumed that any person uses letters of the alphabet with the same RELATIVE FREQUENCYfrom document to document and no two letters are used with the same frequency. That is, the most frequently used letter in the first line maps to the most frequently used letter in the second one; the second most frequently used letter maps to the second most frequently used letter and so on.
The outputs of two consecutive cases will be separated by a blank line.
Sample Intput
1 abacxbacac qqqqqrrrrssstt
Sample Output
aaaaaccccbbbxx
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
public class Main {
public static final boolean DEBUG = false;
public static final int N = 100000;
public BufferedReader cin;
public PrintWriter cout;
// public StreamTokenizer tokenizer;
public StringTokenizer tokenizer;
public String s, t;
public void init() {
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("e:\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
// tokenizer = new StreamTokenizer(cin);
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StringTokenizer("");
} catch (Exception e) {
e.printStackTrace();
}
}
public String next() {
try {
/*
* tokenizer.nextToken(); if (tokenizer.ttype ==
* StreamTokenizer.TT_EOF) return null; else if (tokenizer.ttype ==
* StreamTokenizer.TT_NUMBER) { return
* String.valueOf((int)tokenizer.nval); } else return
* tokenizer.sval;
*/
while (!tokenizer.hasMoreTokens()) {
String s = cin.readLine();
// System.out.println("s:" + s + " " + (s == null));
if (s == null)
return null;
tokenizer = new StringTokenizer(s);
}
return tokenizer.nextToken();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input() {
// next();
s = next();
t = next();
//System.out.println("s:" + s + " t:" + t);
return true;
}
public void solve(int cas) {
TreeMap<Character, Integer> srcHash = new TreeMap<Character, Integer>();
TreeMap<Character, Integer> dstHash = new TreeMap<Character, Integer>();
for (int i = 0, len = s.length(); i < len; i++) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) {
if (srcHash.containsKey(ch)) {
int cnt = srcHash.get(ch);
srcHash.put(ch, cnt + 1);
} else {
srcHash.put(ch, 1);
}
}
}
for (int i = 0, len = t.length(); i < len; i++) {
char ch = t.charAt(i);
if (Character.isLetter(ch)) {
if (dstHash.containsKey(ch)) {
int cnt = dstHash.get(ch);
dstHash.put(ch, cnt + 1);
} else {
dstHash.put(ch, 1);
}
}
}
TreeMap<Integer, Character> a = new TreeMap<Integer, Character>();
TreeMap<Integer, Character> b = new TreeMap<Integer, Character>();
HashMap<Character, Character> ansMap = new HashMap<Character, Character>();
Iterator<Map.Entry<Character, Integer>> it1 = srcHash.entrySet().iterator();
Iterator<Map.Entry<Character, Integer>> it2 = dstHash.entrySet().iterator();
Iterator<Map.Entry<Integer, Character>> ait = a.entrySet().iterator();
Iterator<Map.Entry<Integer, Character>> bit = b.entrySet().iterator();
while (it1.hasNext()) {
Map.Entry<Character, Integer> entry = it1.next();
char ch1 = entry.getKey();
int ch2 = entry.getValue();
//System.out.println("c:" + ch1 + " cnt:" + ch2);
a.put(ch2, ch1);
}
while (it2.hasNext()) {
Map.Entry<Character, Integer> entry = it2.next();
char ch1 = entry.getKey();
int ch2 = entry.getValue();
b.put(ch2, ch1);
}
ait = a.entrySet().iterator();
bit = b.entrySet().iterator();
while (ait.hasNext()) {
char ch1 = ait.next().getValue();
char ch2 = bit.next().getValue();
ansMap.put(ch2, ch1);
}
StringBuffer sb = new StringBuffer();
for (int i = 0, len = t.length(); i < len; i++) {
char ch = t.charAt(i);
if (Character.isLetter(ch)) {
sb.append(ansMap.get(ch));
} else sb.append(ch);
}
cout.println(sb.toString());
if (cas != 0) cout.println();
cout.flush();
}
public static void main(String[] args) {
Main solver = new Main();
solver.init();
int t = Integer.parseInt(solver.next());
while (t-- > 0) {
solver.input();
solver.solve(t);
}
}
}