Ancient Cipher (UVA-10340 && POJ-2159)
2017-06-16 18:57
381 查看
Description
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 <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 contains 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 are equal and do not exceed 100.
Output
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
Sample Output
题意:给你两个字符串,可以任意打乱顺序,看是否可以一一对应;
思路:提取每个字母的个数,排序,看是否能相等;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[30],b[30];
string s1,s2;
int main()
{
while(cin>>s1>>s2)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int len=max(s1.length(),s2.length());
for(int i=0; i<len; i++)
{
a[s1[i]-'A']++;
b[s2[i]-'A']++;
}
sort(a,a+26);
sort(b,b+26);
bool flag=true;
for(int i=0; i<26; i++)
{
if(a[i]!=b[i])
{
flag=false;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
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 <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 contains 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 are equal and do not exceed 100.
Output
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
Sample Output
YES
题意:给你两个字符串,可以任意打乱顺序,看是否可以一一对应;
思路:提取每个字母的个数,排序,看是否能相等;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[30],b[30];
string s1,s2;
int main()
{
while(cin>>s1>>s2)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int len=max(s1.length(),s2.length());
for(int i=0; i<len; i++)
{
a[s1[i]-'A']++;
b[s2[i]-'A']++;
}
sort(a,a+26);
sort(b,b+26);
bool flag=true;
for(int i=0; i<26; i++)
{
if(a[i]!=b[i])
{
flag=false;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
相关文章推荐
- 【原】 POJ 2159 Ancient Cipher 题意万岁 解题报告
- POJ 2159 Ancient Cipher
- Splay Tree (POJ 3580 & UVA 12356 & HDU 1890)
- uva 1394&poj 3517
- uva 1394&poj 3517
- POJ 2159 Ancient Cipher(字符频率统计)
- poj 1271 && uva 10117 Nice Milk (半平面交)
- UVA 514 - Rails && POJ 1363 (堆栈模拟)
- UVa 10042 && POJ 1142 - Smith Numbers
- Uva 10583 && Poj 2524 Ubiquitous Religions
- poj 2159 Ancient Cipher
- POJ 2159 Ancient Cipher(我的水题之路——双重加密)
- POJ 2159 Ancient Cipher
- poj 2159 Ancient Cipher 简单的密码转化问题
- POJ 2159 Ancient Cipher
- POJ-2159-Ancient Cipher-解题报告
- POJ 2159 Ancient Cipher 解题报告
- POJ 2159 Ancient Cipher
- POJ 2159 Ancient Cipher
- POJ&nbsp;2159