本来是想到poj灌几道水题的,没想到poj的题水题也很有分量。
开始的时候原文题意我都没能看出来。
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".
就加色的这句没注意,整个题意都看错了,我还以为所有的都是要是起字符的下一个字符处理。同样第二种编码的方式我也弄错了。这道题其实是转了个弯,题意只给了几个例子结果,真正的算法和题析都得靠自己去分析出来才能得到正确的结论,写出自己的代码来。
#include <stdio.h>
#include <string.h>
int main()
{
int i,j,len1,len2;
char ch1[110],ch2[110];
int a[30],b[30];
while(scanf("%s %s",ch1,ch2)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
len1 = strlen(ch1);
len2 = strlen(ch2);
if(len1!=len2){
printf("NO/n");
continue; //
}
for(i=0;i<len1;i++) //计算出现的字符出现的次数
{
a[ch1[i]-'A']++;
b[ch2[i]-'A']++;
}
for(i=0;i<26;i++){ // 统计字符串ch1和ch2中字符串的个数是否都能对应
for(j=0;j<26;j++) //
if(a[i]==b[j])
{
b[j] = 0;
break; //有对应的就给对应过的清除,然后跳出
}
if(j==26)
{
printf("NO/n"); //如果没有对应的,则输出结束
break;
}
}
if(i==26)
printf("YES/n"); //都有对应的则输出正确
}
return 0;
}
本题其实以题意提供的编码方式没有改变每个字符的出现个数和字符数,所以可以通过判段字符的个数和每个字符的个数来确定源码与密码是否一致。