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个各自种类的字母。

 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 }

时间: 2024-10-20 09:48:10

UVa1339的相关文章

UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher

Regionals 2004 >> Europe - Northeastern 问题链接:UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher.基础训练题,用C语言编写程序. 对两组字符串分别进行字母统计,接着对统计结果进行排序,然后对排序后的结果进行比较.如果相同,说明可以找到一种一一映射,使得两个字符串相同. AC的C语言程序如下: /* UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher *

uva--1339 - Ancient Cipher(模拟水体系列)

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

4_1 古老的密码(UVa1339)

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

算法练习(一)

昨天是个值得纪念的日子,我数学建模拿了推荐国家一等奖的名额,希望最后能顺利拿到国一吧.现在大三已经开学一个月了.这一个月因为社会实践评优的事情真的很忙,还好最后拿到了可能拿到的所有的奖项.结果自己把科研助手这件事给耽误了,今天去找马老师,结果马老师的实验室人已经满了.所以没办法,我可能又要去找其他老师了. 今年国家奖学金的名额里面没有我,没有就算了吧.卧薪尝胆,好好学习,这一学期至关重要.所以自己现在就要开始准备保研的机试,现在的训练非常重要,无论如何,这是自己未来要走的一步路.我现在最重要的就

算法竞赛入门经典 第四章

[√ ] UVA1339 古老的密码 Ancient Cipher [√ ] UVA489 刽子手的游戏 Hangman Judge [√ ] UVA133 救济金发放 The Dole Queue [√ ] UVA213 信息解码 Message Decoding [√ ] UVA512 追踪电子表格中的单元格 Spreadsheet Tracking [√ ] UVA12412 师兄帮帮忙 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)