Poj.1107 W's Cipher【水题】 2015/04/22

来源:互联网 发布:网络诈骗的手段有 编辑:程序博客网 时间:2024/06/05 14:16
W's Cipher
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5227 Accepted: 2633

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_kxert1 1 1bcalmkyzx3 7 4wcb_mxfep_dorul_eov_qtkrhe_ozany_dgtoh_u_eji2 4 3cjvdksaltbmu0 0 0

Sample Output

the_quick_brown_foxabcklmxyzthe_quick_brown_fox_jumped_over_the_lazy_dogajsbktcludmv

Source

Mid-Central USA 2001
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int k1,k2,k3;int l,i,j,ai,bi,ci;char s[100];struct node{char name;int x;}a[100],b[100],c[100];void change(int k,node p[],int len){k %= len;for( i = 0 ; i < len ; ++i ){j = (len-k+i)%len;s[p[i].x] = p[j].name;}}void solve(){l = strlen(s);ai = bi = ci = 0;for( i = 0 ; i < l ; ++i ){if( s[i] >= 'a' && s[i] <= 'i' ){a[ai].name = s[i];a[ai].x = i;ai++;}else if( s[i] >= 'j' && s[i] <= 'r' ){b[bi].name = s[i];b[bi].x = i;bi++;}else{c[ci].name = s[i];c[ci].x = i;ci++;}}change(k1,a,ai);change(k2,b,bi);change(k3,c,ci);printf("%s\n",s);}int main(){while( ~scanf("%d %d %d",&k1,&k2,&k3)&&(k1!=0&&k2!=0&&k3!=0) ){getchar();scanf("%s",s);solve();}return 0;}
注:题目大意:输入三个数字k1,k2,k3,接下来是一行字符(小写字母和下划线“_”),字符串中的字符分成三组,一组是[a,i],二组是[j,r],三组为剩余的,然后第一组按k1往后移位,然后再按移位后的顺序放回到原字符串中,二三组同理,最后输出移位后的字符串
0 0
原创粉丝点击