代码改变世界

POJ 2159 解题报告

2011-10-14 22:29  诸葛二牛  阅读(289)  评论(0编辑  收藏  举报

一、substitution 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".

“移位”只是置换密码的一种,只要满足“Substitutes for all letters must be different.”就是置换了,比如

A->B

C->H

Z->D

对于这道题,input:

AAABB
CCCEE

应输出

YES

所以明文与密文的“字母频率的数组”应该是一样的,即明文中某字母出现8次,密文中也必须有某个字母出现8次。

所以字母种类,字母频率同时相等时,即被破解。

二、permutation cipher(排列密码):

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".

明文随机排列,所以字母种类和频率都不变。

对于这道题,排列密码考虑和不考虑对解题无影响!

 1 //Code name: POJ2159
2 //OJ: POJ 2159
3 //Language: ANSI C (VS2010)
4 //10-14-2011
5 //Status: AC
6 #include<stdio.h>
7 #include<string.h>
8 #include<stdlib.h>
9 const int MAXSIZE=110;
10 int cmp(const void *a, const void *b)
11 {
12 return (*(int*)a- *(int*)b);
13 }
14 int main(){
15 char arr[2][MAXSIZE];
16 int cnt1[26],cnt2[26];
17 char len1,len2,i,j,flag;
18 while(gets(arr[0])!=NULL && gets(arr[1])!=NULL)
19 {
20 len1 =strlen(arr[0]); len2 = strlen(arr[1]);
21 if(len1!=len2) {
22 printf("NO\n");
23 continue;
24 }
25 memset(cnt1,0, sizeof cnt1);
26 memset(cnt2,0, sizeof cnt2);
27 for(i=0; i<len1;i++)
28 {
29 cnt1[arr[0][i]-'A']++;
30 cnt2[arr[1][i]-'A']++;
31 }
32 qsort(cnt1,26,sizeof cnt1[0], cmp);
33 qsort(cnt2,26,sizeof cnt2[0], cmp);
34 for( i=0;i<26 && cnt1[i]==cnt2[i];)
35 i++;
36 if(i<26)printf("NO\n");
37 else printf("YES\n");
38 }
39 return 0;
40 }