题目
本道题来自北京大学的POJ系统,题号2159,题名Ancient Cipher。
链接: POJ 2159
对于题目的理解
这道题翻译过来叫古代密码,主要讲了两种字母的加密方式,一是替换,二是乱序;题目要求判断输入的第一行是否是第二行输入的加密形式,因为替换采用的方式是一一映射的形式,所以字母出现的次数必然是一致的,再对字母出现的次数进行排序,看是否一致,以此进行判断。
代码
题目的输入输出范例
Sample Input:
JWPUDJSTVP
VICTORIOUS
Sample Output:
YES
源代码
import java.util.Arrays;
import java.util.Scanner;
/**
* @author Exticntion
*/
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String code = input.nextLine();
String answer = input.nextLine();
int[] countCode = new int[26];
int[] countAnswer = new int[26];
for (int i = 0; i < countCode.length; i++) {
countCode[i] = countAnswer[i] = 0;
}
// 记录每个字母出现的次数
for (int i = 0; i < code.length(); i++) {
countCode[code.charAt(i) - 'A']++;
countAnswer[answer.charAt(i) - 'A']++;
}
// 升序排列
Arrays.sort(countCode);
Arrays.sort(countAnswer);
boolean result = true;
for (int i = 0; i < countCode.length; i++) {
if (countCode[i] != countAnswer[i]) {
result = false;
break;
}
}
if (result) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}