字符串分组轮换。HOJ1634 W's Cipher。

用stl list实现字符串的轮换。

用stl map建立字母与位置之间的对应。

W's Cipher


Time limit: 1sec. Submitted: 48
Memory limit: 32M Accepted: 32
Source: ACM ICPC Mid-Central USA 2001

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.

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

说明:
将26个字母分为 a-i  j-r  s-z三组。 下划线属于第三组。 给出每组 字符向右轮换 的次数。
组内全部轮换后输出交换后的结果。
用三组map存每组字符的初始位置。三组list存字符。
list实现轮换后 map中建立位置与相应字符间的对应。
搜索位置依次输出即可。
代码如下:
#include <iostream>
#include
<stdio.h>
#include
<map>
#include
<list>
using namespace std;

int main() {
int k[3];
while (cin >> k[0] >> k[1] >> k[2]) {
map
<int, char> gp[3];
list
<char> w[3];
if (k[0] == 0 && k[1] == 0 && k[2] == 0)break;
getchar();
char ch;
int n = 0;
while ((ch = getchar()) != '\n') {
if (ch >= 'a' && ch <= 'i') {
gp[
0][n];
w[
0].push_back(ch);
}
else if (ch >= 'j' && ch <= 'r') {
gp[
1][n];
w[
1].push_back(ch);
}
else {
gp[
2][n];
w[
2].push_back(ch);
}
n
++;
}
for (int i = 0; i < 3; i++) {
if (w[i].empty())continue;
k[i]
%= (w[i].size());
for (int j = 0; j < k[i]; j++) {
w[i].push_front(w[i].back());
w[i].pop_back();
}
map
<int, char>::iterator it = gp[i].begin();
for (it; it != gp[i].end(); it++) {
it
->second = *(w[i].begin());
w[i].pop_front();
}
}
for (int i = 0; i < n; i++) {
if (gp[0].find(i) != gp[0].end())cout << gp[0][i];
else if (gp[1].find(i) != gp[1].end())cout << gp[1][i];
else cout << gp[2][i];
}
cout
<< endl;
}
return 0;
}
posted @ 2011-06-10 10:37  归雾  阅读(348)  评论(0编辑  收藏  举报