UVa468 - Key to Success(水题)
2014-07-24 13:27
246 查看
Key to Success |
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); } } }
相关文章推荐
- UVA10071 Back to High School Physics【水题】
- Hard work is the key to success
- 每日英语:Why Are Fractions Key To Future Math Success?
- HDU1200 POJ2039 ZOJ2208 UVALive3084 To and Fro【密码+水题】
- Road to Success
- how to enumerate subkey names under an arbitrary key
- MySQL: Incorrect key file, try to repair it
- The Secrets to S&OP Success
- A key to happiness
- SOLUTION MANAGER KEY GENERATION(THANKS TO QIANGSHENG)
- 8 Ways to Achieve Success in 2008[转贴]
- Another way to retrieve a custom key's value from web.config in web form application
- how to sign an assembly with a strong name & how to create a pair key怎样通过强名给程序集签名以及如何创建钥匙对?
- How to Assign Shortcut Key to Buttons created via VSTO Excel 2007 Add In
- 系统启动时提示“ntldr is missing press any key to reboot”的处理
- 如何让VS.net中的控制台程序出现Press any key to continue字样?
- 【摘要】Advanced Serialization: Format Your Way to Success with the .NET Framework Versions 1.1 and 2.0
- ORA-01779: cannot modify a column which maps to a non-key-preserved table