九度oj 题目1485:W's Cipher

来源:互联网 发布:es6循环json 编辑:程序博客网 时间:2024/05/21 09:00

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.

样例输入:
2 3 1_icuo_bfnwhoq_kxert1 1 1bcalmkyzx3 7 4wcb_mxfep_dorul_eov_qtkrhe_ozany_dgtoh_u_eji2 4 3cjvdksaltbmu0 0 0
样例输出:
the_quick_brown_foxabcklmxyzthe_quick_brown_fox_jumped_over_the_lazy_dogajsbktcludmv
#include<iostream>#include<string>using namespace std;int main(){    string s;    int k1, k2, k3; //每组字母数量     int i;    int c1[100], c2[100], c3[100]; //存储每组中各字母在原字符串位置     while (cin >> k1 >> k2 >> k3 && k1 && k2 && k3) {          cin >> s;          int j1 = 0, j2=0, j3=0;         for (i=0; i<s.size(); i++) {            if (s[i]>='a' && s[i]<='i') {                c1[j1]=i;                j1++;            }            else if(s[i]>='j'&&s[i]<='r')  {                c2[j2]=i;                j2++;            }            else if((s[i]>='s'&&s[i]<='z')||s[i]=='_') {                c3[j3]=i;                j3++;            }        }    string st(s.size(), 0);    for (int i=0; i <j1; ++i)       st[ c1[(i+k1)%j1] ] = s[c1[i]]; //在每组内右移ki位得到的组内位置对应位置     for (int i=0; i <j2; ++i)       st[ c2[(i+k2)%j2] ] = s[c2[i]];    for (int i=0; i <j3; ++i)       st[ c3[(i+k3)%j3] ] = s[c3[i]];       cout << st << endl;    }    return 0;}         


0 0