题目描述
Weird Wally's Wireless Widgets, Inc. manufactures an eclectic assortment of small, wireless, network capable devices, ranging from dog collars, to pencils, to fishing bobbers. All these devices have very small memories. Encryption algorithms like Rijndael, the candidate for the Advanced Encryption Standard (AES) are demonstrably secure but they don't fit in such a tiny memory. In order to provide some security for transmissions to and from the devices, WWWW uses the following algorithm, which you are to implement. Encrypting a message requires three integer keys, k1, k2, and k3. The letters [a-i] form one group, [j-r] a second group, and everything else ([s-z] and underscore) the third group. Within each group the letters are rotated left by ki positions in the message. Each group is rotated independently of the other two. Decrypting the message means doing a right rotation by ki positions within each group. Consider the message the_quick_brown_fox encrypted with ki values of 2, 3 and 1. The encrypted string is _icuo_bfnwhoq_kxert. The figure below shows the decrypting right rotations for one character in each of the three character groups.
Looking at all the letters in the group [a-i] we see {i,c,b,f,h,e} appear at positions {2,3,7,8,11,17} within the encrypted message. After a right rotation of k1=2, these positions contain the letters {h,e,i,c,b,f}. The table below shows the intermediate strings that come from doing all the rotations in the first group, then all rotations in the second group, then all the rotations in the third group. Rotating letters in one group will not change any letters in any of the other groups.
All input strings contain only lowercase letters and underscores(_). Each string will be at most 80 characters long. The ki are all positive integers in the range 1-100.
输入描述:
Input consists of information for one or more encrypted messages. Each problem begins with one line containing k1, k2, and k3 followed by a line containing the encrypted message. The end of the input is signalled by a line with all key values of 0.
输出描述:
For each encrypted message, the output is a single line containing the decrypted string.
示例1
输入
2 3 1
_icuo_bfnwhoq_kxert
1 1 1
bcalmkyzx
3 7 4
wcb_mxfep_dorul_eov_qtkrhe_ozany_dgtoh_u_eji
2 4 3
cjvdksaltbmu
0 0 0
输出
the_quick_brown_fox
abcklmxyz
the_quick_brown_fox_jumped_over_the_lazy_dog
ajsbktcludmv
思路
又是一道英文题,读起来挺费劲的,不过意思没那么复杂,我们来按照题目的意思模拟一遍解密过程
使用题目中的加密字符串 _icuo_bfnwhoq_kxert,过程如图 1 所示。
了解了题意之后,这道题实现起来看上去不是很难,用一个 num 数组存放字符串每一位的归属组数,然后把字符串中的每一组分别放到三个数组,对三个数组分别进行解密,然后根据 num 数组的顺序将三个数组中的字符依次弹出到结果数组,结果数组便是解密的字符串。
解法
#include <stdio.h>
#include <string.h>
int main() {
for (int k1, k2, k3; ~scanf("%d %d %d", &k1, &k2, &k3);) {
char str[81]; //存输入字符串
int num[81]; //存每个字符的归属组
char str1[81] = "", str2[81] = "", str3[81] = ""; //3 个字符串分别存 3 组数据
char cstr1[81] = "", cstr2[81] = "", cstr3[81] = ""; //3 个字符串分别存解密后的 3 组数据
char res[81] = ""; //存放解密后的结果字符串
int x1 = 0, x2= 0, x3 = 0; //控制每组字符串和结果字符串自增输入
scanf("%s", str); //经提醒才注意到字符串输入时不用加 &
for (int i = 0; i < strlen(str); i++) { //把 str 的每个位置的分组记录到 num 数组,并且归类成 3 组
if (str[i] >= 'a' && str[i] <= 'i') {
num[i] = 1;
str1[x1++] = str[i];
}
else if (str[i] >= 'j' && str[i] <= 'r') { //开始犯了个错误这里只写了 if,导致第一个 if 执行的话 else 也会执行,这里说明了 else if 的意义
num[i] = 2;
str2[x2++] = str[i];
}
else {
num[i] = 3;
str3[x3++] = str[i];
}
}
x1 = 0;
x2 = 0;
x3 = 0;
for (int i = 0; i < strlen(str1); i++) //解密第一组
cstr1[(i + k1) % strlen(str1)] = str1[i];
for (int i = 0; i < strlen(str2); i++) //解密第二组
cstr2[(i + k2) % strlen(str2)] = str2[i];
for (int i = 0; i < strlen(str3); i++) //解密第三组
cstr3[(i + k3) % strlen(str3)] = str3[i];
for (int i = 0; i < strlen(str); i++) { //各组字符分别按照顺序归入结果数组
if (num[i] == 1)
res[i] = cstr1[x1++];
if (num[i] == 2)
res[i] = cstr2[x2++];
if (num[i] == 3)
res[i] = cstr3[x3++];
}
for (int i = 0; i < strlen(str); i++)
printf("%c", res[i]);
printf("\n");
}
return 0;
}
这道题并不是很难,因为没有涉及巧妙的算法,但是却能耗费很多的时间,首先是英文题干很长,理解不容易,然后是代码虽然逻辑清晰,但很长,又涉及很多的数组和变量,极其容易出错。