POJ1107 ZOJ1042 UVALive2291 W's Cipher【密码+模拟】

281 篇文章 4 订阅
232 篇文章 0 订阅

W’s Cipher
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5811 Accepted: 2903

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

Regionals 2001 >> North America - Mid-Central USA

问题链接POJ1107 ZOJ1042 UVALive2291 W’s Cipher
问题简述:(略)
问题分析
    这个问题是一个密码问题,用模拟法来实现,按照题意模拟即可。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* POJ1107 ZOJ1042 UVALive2291 W's Cipher */

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

#define N 80
char s[N + 1], t1[N + 1], t2[N + 1], t3[N + 1], s1[N + 1], s2[N + 1];

void fix(char t[],int k,int len)
{
    int i, j;
    k = len - k % len;
    for(i = 0; i < k; i++)
        s1[i] = t[i];
    s1[i]='\0';

    for(i = k, j = 0; i < len; i++)
        s2[j++] = t[i];
    s2[j]='\0';
    strcat(s2, s1);
    strcpy(t, s2);
}

int main(void)
{
    int k1, k2, k3, len, i1, i2, i3, i;
    while(~scanf("%d%d%d", &k1, &k2, &k3) && (k1 || k2 || k3)) {
        memset(t1, 0, sizeof(t1));
        memset(t2, 0, sizeof(t2));
        memset(t3, 0, sizeof(t3));

        scanf("%s", s);
        len = strlen(s);
        i1 = i2 = i3 = 0;
        for(i = 0; i < len; i++)
            if(s[i] >= 'a' && s[i] <= 'i')  t1[i1++] = s[i];
            else if(s[i] >= 'j' && s[i] <= 'r') t2[i2++] = s[i];
            else if( (s[i] >= 's' && s[i] <= 'z' ) || s[i] == '_') t3[i3++] = s[i];

        if(i1)
            fix(t1, k1, i1);
        if(i2)
            fix(t2, k2, i2);
        if(i3)
            fix(t3, k3, i3);

        i1 = i2 = i3 = 0;
        for(i = 0; i < len; i++)
            if(s[i] >= 'a' && s[i] <= 'i')  s[i] = t1[i1++];
            else if(s[i] >= 'j' && s[i] <= 'r') s[i] = t2[i2++];
            else if( (s[i] >= 's' && s[i] <= 'z' ) || s[i] == '_') s[i] = t3[i3++];

        printf("%s\n", s);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值