UVa1339
1339 Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret
service department. Important documents were sent between provinces and the capital in encrypted
form to prevent eavesdropping. The most popular ciphers in those times were so called substitution
cipher and permutation cipher.
Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all
letters must be different. For some letters substitute letter may coincide with the original letter. For
example, applying substitution cipher that changes all letters from `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 letters of the message. For example, ap-
plying the permutation ⟨2; 1; 5; 4; 3; 7; 6; 10; 9; 8⟩ to the message \VICTORIOUS" one gets the message
\IVOTCIRSUO".
It was quickly noticed that being applied separately, both substitution cipher and permutation
cipher were rather weak. But when being combined, they were strong enough for those times. Thus,
the most important messages were rst encrypted using substitution cipher, and then the result was
encrypted 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 rst glance it
seemed completely meaningless, so it was suggested that the message was encrypted with some substi-
tution and permutation ciphers. They have conjectured the possible text of the original message that
was encrypted, and now they want to check their conjecture. They need a computer program to do it,
so you have to write one.
Input
Input le contains several test cases. Each of them consists of two lines. The rst line contains the
message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so
the encrypted message contains only capital letters of the English alphabet. The second line contains
the original message that is conjectured to be encrypted in the message on the rst line. It also contains
only capital letters of the English alphabet.
The lengths of both lines of the input le are equal and do not exceed 100.
Output
For each test case, print one output line. Output `YES' if the message on the rst line of the input le
could 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
Universidad de Valladolid OJ: 1339 { Ancient Cipher 2/2
NEERCISTHEBEST
SECRETMESSAGES
Sample Output
YES
NO
YES
YES
NO
题意:
有两种古老的信息加密方式。设一给定字符串(里面仅包括大写字母)是要传递的信息,一种加密方式是打乱字符串中所有字符的原有位置并重新排列,从而得到一个新字符串(例如:ABCDEFGàACGDEBF);另一种加密方式就是将字符串的所有大写字母都变成字母表向前移动一位或者数位的哪一个字母(例如:AàC、BàD、XàZ、YàA、等等)从而得到一个新字符串。
输入:
多组数据、每组两行字符串(仅含大写字母)。第一行为加密的字符串、第二行为原始字符串。
输出:
对应每组数据,判断第一行字符串是否有可能是第二行字符串通过题目中的两种加密方式共同加密后得到的加密字符串。
分析:
模拟题。如果第一行为第二行的加密,那么两行字符串长度相同,两字符串所包含的字母的种类数量相同,并且如果一个字符串内含有num个某种字母的某种种类共有cnt个,那么另一字符串内必然存在有cnt个字母种类,它们都恰包含有num个各自种类的字母。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 char cstr[110],str[110]; 8 int alphab[30]; 9 int p1[102],p2[102]; 10 int main(){ 11 while(scanf("%s\n%s",cstr,str) != EOF){ 12 //printf("%s %s\n",cstr,str); 13 memset(alphab,0,sizeof alphab); 14 memset(p1,0,sizeof p1); memset(p2,0,sizeof p2); 15 int len1 = strlen(cstr),len2 = strlen(str); 16 if(len1 != len2){ 17 printf("NO\n"); 18 continue; 19 } 20 int len = len1; 21 int cnt = 0; 22 for(int i = 0 ; i < len ; i++){ 23 alphab[cstr[i] - 'A']++; 24 } 25 for(int i = 0 ; i < 30 ; i++){ 26 p1[alphab[i]]++; 27 } 28 memset(alphab,0,sizeof alphab); 29 for(int i = 0 ; i < len ; i++){ 30 alphab[str[i] - 'A']++; 31 } 32 for(int i = 0 ; i < 30 ; i++){ 33 p2[alphab[i]]++; 34 } 35 bool ok = true; 36 for(int i = 0 ; i < 102 ; i++){ 37 if(p1[i] != p2[i]){ 38 ok = false; 39 break; 40 } 41 } 42 if(ok) 43 printf("YES\n"); 44 else 45 printf("NO\n"); 46 } 47 return 0; 48 }