您的位置:首页 > 其它

POJ 2159(古代加密) 解题报告

2011-03-15 23:33 633 查看
/*_____________________________________POJ 2159题____________________________________________
Ancient Cipher
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 4701  Accepted: 1905

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:

JWPUDJSTVP
VICTORIOUS

Sample Output:

YES
____________________________________________________________________________________________*/

#include<stdio.h>
#include<string.h>

void Sort(int p[],int n) //冒泡排序,从大到小
{
int temp;
int flag=0;
for(int i=1; i<n; i++)
{
flag=0;
for(int j=0; j<n-i; j++)
if(p[j]<p[j+1])
{
flag=1;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
if(flag==0)
break;
}
}

int main()
{
int i;
char encrypted[110]={'/0'}; //密文
char supposed[110]={'/0'};  //猜想的明文
int e_count[26]={0};  //统计密文中每个字符的出现次数
int s_count[26]={0};  //统计猜想的明文中每个字符的出现次数
//	FILE *fin=fopen("input.txt","r");
//	fscanf(fin,"%s",encrypted);
//	fscanf(fin,"%s",supposed);
scanf("%s",encrypted);
scanf("%s",supposed);

for(i=0; i<strlen(supposed); i++)
{
s_count[supposed[i]-'A']++;  //s_count[0]存放supposed[]中A的出现次数,以此类推
e_count[encrypted[i]-'A']++; //e_count[0]存放encrypted[]中A的出现次数,以此类推
}
Sort(s_count,26);  //从大到小排序
Sort(e_count,26);
/*	for(i=0;i<26;i++)
printf("%d ",s_count[i]);
printf("/n");
for(i=0;i<26;i++)
printf("%d ",e_count[i]);
printf("/n");
*/
for(i=0; i<26 && e_count[i]==s_count[i]; i++);  //比较e_count[]和s_count[]是否完全相同
if(i==26)
printf("YES");
else
printf("NO");

return 0;
}

//原来我以为“替换”只能是在26个大写英文字母之间平移,后来发现“替换”可以是字母之间的任意映射,
//所以只要统计明文和密文中各字母的出现次数,若出现次数序列完全相同,则此明文可加密为此密文
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: