描述
加密一条信息需要三个整数码, k1, k2 和 k3。字符[a-i] 组成一组, [j-r] 是第二组, 其它所有字符 ([s-z] 和下划线)组成第三组。 在信息中属于每组的字符将被循环地向左移动ki个位置。 每组中的字符只在自己组中的字符构成的串中移动。解密的过程就是每组中的字符在自己所在的组中循环地向右移动ki个位置。
例如对于信息 the_quick_brown_fox 以ki 分别为 2, 3 和 1蔼进行加密。加密后变成 _icuo_bfnwhoq_kxert。下图显示了右旋解密的过程。

观察在组[a-i]中的字符,我们发现{i,c,b,f,h,e}出现在信息中的位置为{2,3,7,8,11,17}。当k1=2右旋一次后, 上述位置中的字符变成{h,e,i,c,b,f}。下表显示了经过所有第一组字符旋转得到的中间字符串,然后是所有第二组,第三组旋转的中间字符串。在一组中变换字符将不影响其它组中字符的位置。

所有输入字符串中只包含小写字母和下划线(_)。所有字符串最多有偿服务0个字符。ki 是1-100之间的整数。
输入
输入包括一到多组数据。每个组前面一行包括三个整数 k1, k2 和 k3,后面是一行加密信息。输入的最后一行是由三个0组成的。
输出
对于每组加密数据,输出它加密前的字符串。
样例输入
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
分析:
按题意进行模拟,先拆分密码再组装明文。此题比较简单,注意某组没有元素时,容易导致RE。RE时,应当首先检查此种情况。
//冰非寒(binfeihan@126.com)
// 252kB 0ms 1950 B G++
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int const N = 200;
struct Map{
char value[N];
int address[N];
int k;
};
class Plaintext{
public:
Plaintext(const int,const int,const int);
void decrypt(const string);
void rightmove(Map &,int);
void print();
private:
int key_1;
int key_2;
int key_3;
string text;
};
Plaintext::Plaintext(const int k1,const int k2,const int k3){
key_1 = k1;
key_2 = k2;
key_3 = k3;
}
void Plaintext::decrypt(const string cipher){
int clen = cipher.length();
Map map[3];
map[0].k = 0;
map[1].k = 0;
map[2].k = 0;
for(int i(0);i < clen;++ i){
if(cipher[i] >= 'a' && cipher[i] <= 'i'){
int j = map[0].k;
map[0].value[j] = cipher[i];
map[0].address[j] = i;
map[0].k ++;
continue;
}
if(cipher[i] >= 'j' && cipher[i] <= 'r'){
int j = map[1].k;
map[1].value[j] = cipher[i];
map[1].address[j] = i;
map[1].k ++;
continue;
}
int j = map[2].k;
map[2].value[j] = cipher[i];
map[2].address[j] = i;
map[2].k ++;
}
rightmove(map[0],key_1);
rightmove(map[1],key_2);
rightmove(map[2],key_3);
//text is made up
text = cipher;
for(int i(0);i < 3;++ i){
for(int j(0);j < map[i].k;++ j){
text[map[i].address[j]] = map[i].value[j];
}
}
}
void Plaintext::rightmove(Map &m,int n){
if(0 == m.k){
return ;
}
int t = n % m.k;
char temp1[N];
char temp2[N];
if(0 == t){
return ;
}
m.value[m.k] = '\0';
strncpy(temp1,m.value,m.k - t);
temp1[m.k - t] = '\0';
strncpy(temp2,m.value + m.k - t,t);
temp2[t] = '\0';
strcat(temp2,temp1);
strcpy(m.value,temp2);
}
void Plaintext::print(){
cout<<text<<endl;
}
int main(){
int k1,k2,k3;
string cipher;
while(cin>>k1>>k2>>k3){
if(0 == k1 && 0 == k2 && 0 == k3){
break;
}
cin>>cipher;
Plaintext ptext(k1,k2,k3);
ptext.decrypt(cipher);
ptext.print();
}
return 0;
}