POJ 1107 W's Cipher

来源:互联网 发布:电子老鼠闯迷宫c语言 编辑:程序博客网 时间:2024/05/21 07:46

题目地址:http://poj.org/problem?id=1107
W’s Cipher
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5524 Accepted: 2761
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

题目大意:
将26个字母及’_’分成三组;
[a,i]为一组,[j,r]为一组,[s,z]和’_’为一组;
给k1,k2,k3对应三组;
将同组向右平移k个单位,且该组在原字符串中所占的下标数量和位置不变

思路:
将三组分别储存在一个新的字符数组中
对原字符串进行遍历
先判断属于哪一组,再输出该组中的第n-k个字符
若坐标<0,再进行处理
之前已对k超出长度的情况进行了处理

#include<cstdio>#include<iostream>#include<cstring>using namespace std;int main(){    int k1,k2,k3;    while(scanf("%d%d%d",&k1,&k2,&k3)!=EOF)    {        if(k1==0&&k2==0&&k3==0)            break;        char str[100],s1[100],s2[100],s3[100];        scanf("%s",&str);        int len=strlen(str);        int len1,len2,len3;        for(int i=0,j=0,k=0,l=0;i<len;i++)        {            if(str[i]>='a'&&str[i]<='i')            {                s1[j]=str[i];                j++;                len1=j;            }            else if(str[i]>='j'&&str[i]<='r')            {                s2[k]=str[i];                k++;                len2=k;            }            else if((str[i]>='s'&&str[i]<='z')||str[i]=='_')            {                s3[l]=str[i];                l++;                len3=l;            }        }        while(k1>=len1)            k1-=len1;        while(k2>=len2)            k2-=len2;        while(k3>=len3)            k3-=len3;        /*for(int i=0;i<len1;i++)            printf("%c",s1[i]);        printf("\n");        for(int i=0;i<len2;i++)            printf("%c",s2[i]);        printf("\n");        for(int i=0;i<len3;i++)            printf("%c",s3[i]);        printf("\n");*/        for(int i=0,j=0,k=0,l=0;i<len;i++)        {            if(str[i]>='a'&&str[i]<='i')            {                if(j-k1<0)                {                    cout << s1[j-k1+len1];                    j++;                }                else                {                    cout << s1[j-k1];                    j++;                }            }            else if(str[i]>='j'&&str[i]<='r')            {                if(k-k2<0)                {                    cout << s2[k-k2+len2];                    k++;                }                else                {                    cout << s2[k-k2];                    k++;                }            }            else if((str[i]>='s'&&str[i]<='z')||str[i]=='_')            {                if(l-k3<0)                {                    cout << s3[l-k3+len3];                    l++;                }                else                {                    cout << s3[l-k3];                    l++;                }            }        }        printf("\n");    }    return 0;}

//是一道比较水的模拟,因为没理解好题意做了好久QAQ

0 0
原创粉丝点击