W's Cipher

 

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.

示例1

输入

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

题目解析:解密过程如下图所示:

 

了解了题意之后,这道题实现起来看上去不是很难,用一个 num 数组存放字符串每一位的归属组数,然后把字符串中的每一组分别放到三个数组,对三个数组分别进行解密,然后根据 num 数组的顺序将三个数组中的字符依次弹出到结果数组,结果数组便是解密的字符串。

代码实现:

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define N 100

typedef struct{
    char s;
    int pos;//用于存储字符s以及它在字符串中的位置
}Node;

Node k1[N];//用于存储[a,i]之间的字符以及它们再字符数组中的位置
Node k2[N];//用于存储[j,r]之间的字符以及它们再字符数组中的位置
Node k3[N];//用于存储[s,z]之间的字符和下划线_以及它们再字符数组中的位置

int main ()
{
    int  c1,c2,c3;
    char S[N];
    while(cin>>c1>>c2>>c3&&c1!=0)
    {
        cin>>S;
        int w1,w2,w3;
        w1=w2=w3=0;
        int n=strlen(S);
        for(int i=0;i<n;i++)
        {
            if('a'<=S[i]&&S[i]<='i')
            {
                k1[w1].s=S[i];
                k1[w1].pos=i;
                w1++;
            }
            else if('j'<=S[i]&&S[i]<='r')
            {
                k2[w2].s=S[i];
                k2[w2].pos=i;
                w2++;
            }
            else
            {
                k3[w3].s=S[i];
                k3[w3].pos=i;
                w3++;
            }
        }//上面整个for循环用于将输入字符数组中的字符进行归类到k1[],k2[],k3[]中
    char temp;
    int j=0;
    int i=w1-c1%w1-1;
    while(j<i)
    {
        temp=k1[j].s;
        k1[j].s=k1[i].s;
        k1[i].s=temp;
        j++;
        i--;
    }
    j=w1-c1%w1;
    i=w1-1;
    while(j<i)
    {
        temp=k1[j].s;
        k1[j].s=k1[i].s;
        k1[i].s=temp;
        j++;
        i--;
    }
    j=0;
    i=w1-1;
    while(j<i)
    {
        temp=k1[j].s;
        k1[j].s=k1[i].s;
        k1[i].s=temp;
        j++;
        i--;
    }//上述代码对K1[]中的字符进行右旋操作
    j=0;
     i=w2-c2%w2-1;
    while(j<i)
    {
        temp=k2[j].s;
        k2[j].s=k2[i].s;
        k2[i].s=temp;
        j++;
        i--;
    }
    j=w2-c2%w2;
    i=w2-1;
    while(j<i)
    {
        temp=k2[j].s;
        k2[j].s=k2[i].s;
        k2[i].s=temp;
        j++;
        i--;
    }
    j=0;
    i=w2-1;
    while(j<i)
    {
        temp=k2[j].s;
        k2[j].s=k2[i].s;
        k2[i].s=temp;
        j++;
        i--;
    }//上述代码对K2[]中的字符进行右旋操作
    j=0;
     i=w3-c3%w3-1;
    while(j<i)
    {
        temp=k3[j].s;
        k3[j].s=k3[i].s;
        k3[i].s=temp;
        j++;
        i--;
    }
    j=w3-c3%w3;
    i=w3-1;
    while(j<i)
    {
        temp=k3[j].s;
        k3[j].s=k3[i].s;
        k3[i].s=temp;
        j++;
        i--;
    }
    j=0;
    i=w3-1;
    while(j<i)
    {
        temp=k3[j].s;
        k3[j].s=k3[i].s;
        k3[i].s=temp;
        j++;
        i--;
    }//上述代码对K3[]中的字符进行右旋操作
        for(i=0;i<w1;i++)
        {
            S[k1[i].pos]=k1[i].s;
        }
        for(i=0;i<w2;i++)
        {
            S[k2[i].pos]=k2[i].s;
        }
        for(i=0;i<w3;i++)
        {
            S[k3[i].pos]=k3[i].s;
        }//将旋转后的结果放回数组S中
        cout<<S<<endl;
    }
    return 0;
}

结果展示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值