# POJ 1107 W’s Cipher C++版

POJ题解 1628浏览

Description

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

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.

Output

For each encrypted message, the output is a single line containing the decrypted string.

Sample Input

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

Sample Output

the_quick_brown_fox
abcklmxyz
the_quick_brown_fox_jumped_over_the_lazy_dog
ajsbktcludmv

Slyar:题意理解起来比较难，看了10几分钟才明白…大意就是把字符串按字母分成3组，[a-i]一组，[j-r]一组，[s-z]以及其他字符一组，然后给出了3个整数表示每组内的轮换间隔，让你在各组内分别旋转…哎，自己多看看就明白了。

```#include
#include
#include

using namespace std;

int main()
{
int i, pos;
int k1, k2, k3;
string str;
char dstr[81];
vector n1, n2, n3;

while(1)
{
cin >> k1 >> k2 >> k3;
if (k1 + k2 + k3 == 0) break;

cin >> str;

/* 清空容器 */
n1.clear();
n2.clear();
n3.clear();

/* 记录每个字符的位置 */
for (i = 0; i < str.length(); i++)
{
if (str[i] >= 'a' && str[i] <= 'i') n1.push_back(i);
else if (str[i] >= 'j' && str[i] <= 'r') n2.push_back(i);
else n3.push_back(i);
}

/* 把字符填到正确的位置 */
for (i = 0; i < n1.size(); i++)
{
pos = (i + k1) % n1.size();
dstr[n1[pos]] = str[n1[i]];
}
for (i = 0; i < n2.size(); i++)
{
pos = (i + k2) % n2.size();
dstr[n2[pos]] = str[n2[i]];
}
for (i = 0; i < n3.size(); i++)
{
pos = (i + k3) % n3.size();
dstr[n3[pos]] = str[n3[i]];
}

/* 封口 */
dstr[str.length()] = 0;

cout << dstr << endl;
}
//system("pause");
return 0;
}
```