Question link: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4085
13855995 |
1339 |
Ancient Cipher |
Accepted |
C ++ |
0.012 |
12:35:33 |
Ancient Cipher
Specified ent Roman Empire had a strong government system with varous parameters, 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''Ubunious''One gets the message''Wjdupsjpvt''. Permutation cipher applies some permutation to the letters of the message. for example, applying the permutation 2, 1, 5, 4, 3, 7, 6, 10, 9, 8 to the Message''Ubunious''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 first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. encrypting the message''Ubunious''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 the message was encrypted with some substitution 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 file contains several test cases. each of them consists of two lines. the first 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 first line. it also contains only capital letters of the English alphabet. the lengths of both lines of the input file are equal and do not exceed 100.
Output
For each test case, print one output line. output'Yes'If the message on the first line of the input file cocould be the result of encrypting the message on the second line, or'No'In the other case.
Sample Input
JWPUDJSTVPVICTORIOUSMAMAROMEHAHAHEHEAAAAAANEERCISTHEBESTSECRETMESSAGES
Sample output
YESNOYESYESNO
Solution: after reading the question for a long time, I did not expect it to be a Sort question. The general idea is to change the character order in a string before uncertain one-to-one matching to determine whether another string can be obtained.
1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace std;10 int main() {11 string str1, str2;12 int cnt1[26], cnt2[26];13 while (cin >> str1 >> str2) {14 memset(cnt1, 0, sizeof(cnt1));15 memset(cnt2, 0, sizeof(cnt2));16 17 for(int i = 0; i < str1.size(); i++) {18 cnt1[str1[i] - ‘A‘] ++;19 cnt2[str2[i] - ‘A‘] ++;20 }21 22 sort(cnt1, cnt1 + 26);23 sort(cnt2, cnt2 + 26);24 25 bool flag = true;26 27 for(int i = 0 ; i < 26; i++) {28 if(cnt1[i] != cnt2[i]) {29 flag = false;30 break;31 }32 }33 34 if(flag) cout << "YES" << endl;35 else cout << "NO" << endl;36 37 }38 return 0;39 }