欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 1107 W's Cipher题解

程序员文章站 2022-07-12 11:07:50
...

题目来源:http://poj.org/problem?id=1107

 

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.
POJ 1107 W's Cipher题解
            
    
    博客分类: ACM解题报告 J#CC++C#F#
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.
POJ 1107 W's Cipher题解
            
    
    博客分类: ACM解题报告 J#CC++C#F#
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                                                                                                                                           

此题题意是将26个字母和"_"分成3段,a~i为第一段,j~r为第二段,其他的为第三段。拿第一组测试数据来说,分为3组分别为:

t1{i c b f h e}

t2{o n o q k r}

t3{_  u  _  w _  x  t}

k值分别为k1=2,k2=3, k3=1,t1,t2,t3分别向右旋转ki布后得到的新数据分别为:

t1'{h e i c b f}

t2'{q k r o n o}

t3'{t _ u _ w _ x }

将跟换后的新数据替换掉在原字符串的位置即可

#include <stdio.h>
#include <string.h>

void rotate(char str[], int len, int k)
{
    //将字符串向右旋转k个单位
    int i, j;
    char str1[81], str2[81];
    k = len-k%len;
    for(i = 0; i < k; i++)
    {
        str1[i] = str[i];
    }
    str1[i] = '\0';

    j = 0;
    for(i = k; i < len; i++)
    {
        str2[j++] = str[i];
    }
    str2[j] = '\0';

    strcat(str2, str1);
    strcpy(str, str2);
}

int main()
{
    int i;
    int k1, k2, k3;
    int len1, len2, len3, len;
    int n1[81], n2[81], n3[81];
    char t1[81], t2[81], t3[81], str[81];

    while(1)
    {
        scanf("%d %d %d", &k1, &k2, &k3);
        if((k1+k2+k3) == 0)  break;
        scanf("%s", str);
        len = strlen(str);
        len1 = len2 = len3 = 0;
        memset(t1, '\0', sizeof(t1));
        memset(t2, '\0', sizeof(t2));
        memset(t3, '\0', sizeof(t3));

        for(i = 0; i < len; i++)
        {
            if(str[i] >= 'a' && str[i] <= 'i')
            {
                t1[len1] = str[i];
                n1[len1++] = i;
            }
            else if(str[i] >= 'j' && str[i] <= 'r')
            {
                t2[len2] = str[i];
                n2[len2++] = i;
            }
            else
            {
                t3[len3] = str[i];
                n3[len3++] = i;
            }
        }

        if(len1) rotate(t1, len1, k1);
        if(len2) rotate(t2, len2, k2);
        if(len3) rotate(t3, len3, k3);

        for(i = 0; i < len1; i++)
        {
            str[n1[i]] = t1[i];
        }

        for(i = 0; i < len2; i++)
        {
            str[n2[i]] = t2[i];
        }

        for(i = 0; i < len3; i++)
        {
            str[n3[i]] = t3[i];
        }

        printf("%s\n", str);
    }
    return 0;
}