首页 > 其他 > 详细

POJ 1107 W's Cipher 解密

时间:2014-02-22 08:38:11      阅读:425      评论:0      收藏:0      [点我收藏+]
W‘s Cipher
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5009   Accepted: 2513

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. 
bubuko.com,布布扣

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. 
bubuko.com,布布扣

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


给你三个数字k1,k2,k3,然后三组范围分别是1:[a,i],2:[j,r],3:[s-z]和下划线。在信息中的字符属于哪一组就向做移动ki个位置,只能在自己构成的组中移动,当然解密的话就是向右移动ki个位置,现在给你密文,让你输出原文。
记录每个字母属于哪个范围,然后对于每个范围进行相应的移动。
//364K	32MS
#include<stdio.h>
#include<string.h>
#define M 107
char ans[3][M],ss[3][M],s[M];//ans存的是加密字符串,ss存的是加密后
int p[3];
void change(char s1[M],int l,int k,char s2[M])//转换后的位置
{
    for(int i=0;i<l;i++)
        s2[(i+k)%l]=s1[i];
}
int main()
{
    int k1,k2,k3;
    while(scanf("%d%d%d",&k1,&k2,&k3),k1|k2|k3)
    {
        scanf("%s",s);
        int len=strlen(s),l1=0,l2=0,l3=0;
        for(int i=0;i<len;i++)
        {
            if(s[i]>=‘a‘&&s[i]<=‘i‘){p[i]=1;ans[1][l1++]=s[i];}
            else if(s[i]>=‘j‘&&s[i]<=‘r‘){p[i]=2;ans[2][l2++]=s[i];}
            else{p[i]=3;ans[3][l3++]=s[i];}
        }
        if(l1)k1%=l1;
        if(l2)k2%=l2;
        if(l3)k3%=l3;
        change(ans[1],l1,k1,ss[1]);
        change(ans[2],l2,k2,ss[2]);
        change(ans[3],l3,k3,ss[3]);
        l1=l2=l3=0;
        for(int i=0;i<len;i++)
            if(p[i]==1){printf("%c",ss[1][l1++]);}
            else if(p[i]==2){printf("%c",ss[2][l2++]);}
            else if(p[i]==3){printf("%c",ss[3][l3++]);}
        printf("\n");
    }
    return 0;
}



POJ 1107 W's Cipher 解密

原文:http://blog.csdn.net/crescent__moon/article/details/19626155

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!