uva 1339 Ancient Cipher(字符串处理)
2015-02-01 11:25
429 查看
uva 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 thosetimes 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, applying 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 first 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 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 ofthe 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 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 NEERCISTHEBEST SECRETMESSAGES
Sample Output
YES NO YES YES NO
题目大意:给出两个字符串,判断两个字符串中的字符是否一一对应。
解题思路:统计两组字符串中字符出现的次数,然后将次数数组排序,之后一一比较。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char A[105], B[105]; int numA[30], numB[30]; int main() { while (scanf("%s\n%s\n", A, B) == 2) { memset(numA, 0, sizeof(numA)); memset(numB, 0, sizeof(numB)); int lenA = strlen(A); int lenB = strlen(B); if (lenA != lenB) { printf("NO\n"); continue; } for (int i = 0; i < lenA; i++) { numA[A[i] - 'A']++; numB[B[i] - 'A']++; } sort(numA, numA + 26); sort(numB, numB + 26); int flag = 1; for (int i = 0; i < 26; i++) { if (numA[i] != numB[i]) { flag = 0; break; } } if (flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- uva 1339 Ancient Cipher(字符串处理)
- UVA - 1339 Ancient Cipher :简单字符串处理
- uva 1339 Ancient Cipher(字符串处理)
- uva - 1339 - Ancient Cipher(字符串,排序)
- UVA 1339 - Ancient Cipher
- uva 10878 Decode the tape(字符串处理)
- uva 10815 Andy's First Dictionary(字符串处理+sort排序)
- uva 644 Immediate Decodability(字符串处理)
- uva 401 Palindromes(字符串处理)
- UVa 10340 All in All (字符串处理&编程技巧)
- UVa 1339 - Ancient Cipher
- UVA 1339 - Ancient Cipher
- uva 401 Palindromes(基本的字符串处理)
- UVA 1339 - Ancient Cipher
- 暑假集训单切赛第二场 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)(字符串处理)
- NEERC 2004 / UVa 1339 Ancient Cipher (模拟)
- uva 310 L--system(隐式图搜索+字符串处理)
- uva1339 - Ancient Cipher
- uva1339 Ancient Cipher
- uva 409 Excuses, Excuses!(字符串处理)