UVa468 - Key to Success(水题)
来源:互联网 发布:软件开发创业前景 编辑:程序博客网 时间:2024/04/30 08:16
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
1abacxbacacqqqqqrrrrssstt
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);}}}
- UVa468 - Key to Success(水题)
- Hard work is the key to success
- Codeforces Gym 100623K Problem K. Key to Success
- Road to Success
- The Road to Success
- Desire to success
- Essencial to Success
- 67 step to success
- Detail is the key of success
- Doing Enterprise Architecture- EA Key Success Factors
- Negotiate: Use Key Tactics for Success
- The first step to success.....
- How to learn with success
- Success
- success
- success
- success
- success
- No value for key [org.hibernate.impl.SessionFactoryImpl 异常解决
- 百度吹响了走向国际化的号角
- C + +的袖珍参考
- 【比特币】序列化
- 笔记本无法链接网络
- UVa468 - Key to Success(水题)
- Android webbiew 加载百度地图或者其他页面无法正常显示的处理技巧
- 查看表空间使用情况
- gopkg:一种方便的go pakcage管理方式
- 网站如何提升交互体验
- android SQLite数据库相关的操作
- 【比特币】脚本
- input file
- WPF学习笔记(1) 合并资源字典