UVa468 - Key to Success(水题)

 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, tex2html_wrap_inline35 , tex2html_wrap_inline37 , tex2html_wrap_inline39 , tex2html_wrap_inline41 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:

  1. Only letters are encoded. Letters are mapped to letters. Uppercase letters are different from their lowercase counter parts.
  2. The mapping that defines the encoding is one-to-one. That is, two different letters never map to the same letter of the alphabet ( tex2html_wrap_inline43 and tex2html_wrap_inline45 is impossible).
  3. 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.
  4. Both lines are written by the same person.
  5. 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);
		}
	}
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值