UVA 1339 - Ancient Cipher

来源:互联网 发布:csdn修改博客域名 编辑:程序博客网 时间:2024/04/27 22:50

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, 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 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 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 could be the result of encrypting the message on the second line, or `NO' in the other case.

Sample Input 

JWPUDJSTVPVICTORIOUSMAMAROMEHAHAHEHEAAAAAANEERCISTHEBESTSECRETMESSAGES

Sample Output 

YESNOYESYESNO

题目大意:给两个长度相等的字符串,问能否通过排序后 各个字母做一个映射,使得两个字符串相等。
直接举个列:
A:JWPUDJSTVPB:VICTORIOUS
A中出现两次的有2个:J P , 1个的有6个: W U D S T V
B中出现两次的有2个,出现1个的6个。 所以这个可以。

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #include <vector> 6 #include <map> 7 #include <set> 8  9 using namespace std;10 11 #ifdef WIN12 typedef __int64 LL;13 #define form "%I64d\n"14 #else15 typedef long long LL;16 #define form "%lld\n"17 #endif18 19 #define SI(a) scanf("%d", &(a))20 #define S64I scanf(form, &a)21 #define SS(a) scanf("%s", (a))22 #define SC(a) scanf("%c", &(a))23 #define PI(a) printf("%d\n", a)24 #define P64I(a) printf(form, a)25 #define Max(a, b) (a > b ? a : b)26 #define Min(a, b) (a < b ? a : b)27 #define MSET(a, b) (memset(a, b, sizeof(a)))28 #define Mid(L, R) (L + (R - L)/2)29 #define Abs(a) (a > 0 ? a : -a)30 #define REP(i, n) for(int i=0; i < (n); i++) 31 #define FOR(i, a, n) for(int i=(a); i <= (n); i++)32 33 const int maxn = 100 + 20;34 char A[maxn], B[maxn];35 int cnt1[30], cnt2[30];36 int v1[maxn], v2[maxn];37 38 39 int main() {40     while(scanf("%s%s", A, B) == 2) {41         MSET(cnt1, 0);42         MSET(cnt2, 0);43         MSET(v1, 0);44         MSET(v2, 0);45         int n = strlen(A);46         for(int i=0; i < n; i++) {47             cnt1[A[i]-'A']++;48             cnt2[B[i]-'A']++;49         }50         for(int i=0; i<26; i++) v1[cnt1[i]]++, v2[cnt2[i]]++;51         int flag = 1;52         for(int i=1; i<=n; i++) if(v1[i] != v2[i]) {53             flag = 0; break;54         }55         if(flag)56             puts("YES");57         else58             puts("NO");59     }60 61     return 0;62 }

 








原创粉丝点击