古老的密码(Ancient Cipher)

Ancient Cipher

Time limit: 3.000 seconds

Ancient Roman empire had a strong governmentsystem with various departments, including a secret service department.Important documents were sent between provinces and the capitalin encrypted form to prevent eavesdropping. The most popular ciphers inthose times were so called substitution cipher andpermutation cipher.Substitution cipher changes all occurrences of each letter to someother letter. Substitutes for all letters must be different. Forsome letters substitute letter may coincide with the original letter.For example, applying substitution cipher that changes all lettersfrom `A' to `Y' to the next ones in the alphabet,and changes `Z' to `A', to the message ``VICTORIOUS''one gets the message ``WJDUPSJPVT''.Permutation cipher applies some permutation to the lettersof the message. For example, applying the permutation$ \langle$2, 1, 5, 4, 3, 7, 6, 10, 9, 8$ \rangle$ to the message``VICTORIOUS'' one gets the message``IVOTCIRSUO''.It was quickly noticed that being applied separately, both substitutioncipher and permutation cipher were rather weak. But when being combined,they were strong enough for those times. Thus, the most important messages werefirst encrypted using substitution cipher, and then the result wasencrypted using permutation cipher. Encrypting the message ``VICTORIOUS''with the combination of the ciphers described above one gets the message``JWPUDJSTVP''.Archeologists have recently found the message engraved on a stone plate.At the first glance it seemed completely meaningless, so it was suggested that themessage was encrypted with some substitution and permutation ciphers. Theyhave conjectured the possible text of the original message that was encrypted, and nowthey want to check their conjecture. They need a computer program to do it, soyou have to write one.

Input 

Input file contains several test cases. Each of them consists of two lines. The first line contains the messageengraved on the plate. Before encrypting, all spaces and punctuationmarks were removed, so the encrypted message contains only capital letters of theEnglish alphabet.The second line contains the original message that is conjecturedto be encrypted in the message on the first line. It also containsonly capital letters of the English alphabet.The lengths of both lines of the input file are equal and do notexceed 100.

Output 

For each test case, print one output line. Output ` YES' if the message on the first line of the input filecould be the result of encrypting the message on the second line, or` NO' in the other case.

Sample Input 

JWPUDJSTVP
VICTORIOUS
MAMA
ROME
HAHA
HEHE
AAA
AAA
NEERCISTHEBEST
SECRETMESSAGES

Sample Output 

YES
NO
YES
YES
NO

【分析】

       既然字母可以重排,则每个字母的位置并不重要,重要的是每个字母出现的次数。这样可以先统计出两个字符串中各个字母出现的次数,得到两个数组cnt1[26]和cnt2[26]。下一步需要一点想象力:只要两个数组排序之后的结果相同,输入的两个串就可以通过重排和一一映射变得相同。这样,问题的核心就是排序。

用java语言编写程序,代码如下:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		while(input.hasNext()) {
			String s1 = input.nextLine();
			String s2 = input.nextLine();
			
			int[] cnt1 = new int[26];
			int[] cnt2 = new int[26];
			
			for(int i = 0; i < 26; i++) {
				cnt1[i] = cnt2[i] = 0;
			}
			
			for(int i = 0; i < s1.length(); i++) {
				cnt1[s1.charAt(i) - 'A']++;
				cnt2[s2.charAt(i) - 'A']++;
			}
			
			Arrays.sort(cnt1);
			Arrays.sort(cnt2);
			
			boolean result = true;
			for(int i = 0; i < 26; i++) {
				if(cnt1[i] != cnt2[i])
					result = false;
			}
			
			if(result)
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值