文章标题poj 1107:W's Cipher (模拟)

来源:互联网 发布:淄博seo 编辑:程序博客网 时间:2024/05/17 22:52

W’s Cipher

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
Source

Mid-Central USA 2001

题意:给你一个只包含下划线和小写字母的字符串,其中字符【a-i】为一组,【j-r】为一组,剩下的字符为一组。现给你三个数k1,k2,k3。分别代表在所给字符串中的字符往右移动ki个位置,比如,【a-i】这个组中有字符{ i,c,b,f,h,e },他们的位置为{2,3,7,8,11,17},当k1 = 2的时候,变为{h,e,i,c,b,f}。
分析:直接模拟,定义数组pi,(i表示第i组),放置每组中每个的符的位置,然后再根据ki移动字符串。
代码

#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;int p1[85],p2[85],p3[85];int main (){    string a;    int k1,k2,k3;    while (scanf ("%d%d%d",&k1,&k2,&k3)!=EOF){        if (k1==0&&k2==0&&k3==0)break;        cin>>a;        int len=a.length();        int t1=0,t2=0,t3=0;         for (int i=0;i<len;i++){            if (a[i]<='i'&&a[i]>='a'){//记录【a-i】组的字符                p1[t1++]=i;//存放位置            }            else if (a[i]>='j'&&a[i]<='r'){//同上                p2[t2++]=i;            }            else {                p3[t3++]=i;            }        }        string b=a;        for (int i=0;i<t1;i++){            a[p1[(i+k1)%t1]]=b[p1[i]];//将每个字符往后移动k1位,其中得注意i+k1可能超过数组范围,所以得取模        }        for (int i=0;i<t2;i++){//同上            a[p2[(i+k2)%t2]]=b[p2[i]];        }        for (int i=0;i<t3;i++){            a[p3[(i+k3)%t3]]=b[p3[i]];        }        cout << a << endl;     }           return 0;}
0 0