【九度】题目1485:W's Cipher

108 篇文章 0 订阅
102 篇文章 5 订阅
题目地址:http://ac.jobdu.com/problem.php?pid=1485
题目描述:

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.

样例输入:
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
来源:
2012年北京大学计算机研究生机试真题
题目本身不难,但是因为是英文描述,所以感觉有点难。
先说一下题目意思吧。
The letters [a-i] form one group, [j-r] a second group, and everything else ([s-z] and underscore) the third group.
这句话很重要,表示分组,有三个组。
group 1 [a-i]
group 2 [j-r]
group 3 [s-z],_
有3个正数,k1,k2,k3,分别表示的是每个组移动的距离。
然后现在有个规则是这样的。
比如有这样的序列
k1 = 2,k2 = 2,k3 = 1;
_icuo_bfnwhoq_kxert
这个序列中,group 1的有字母,[i,c,b,f,h,e],他们的位置为:[2,3,7,8,11,17]
因为k1 = 2,所以group1的字符都依次循环右移2个位置,移位以后为[h,e,i,c,b,f],
也就是说原来序列中的[2,3,7,8,11,17]位置的字母变为了[h,e,i,c,b,f]。
其他组移位也是类似的。
解决思路应该是扫描原始序列,分成三组存储,存储的内容不是字母,而是位置。
对于每个组,移动以后的位置计算应该是
i表示组内字母的位置,
int curPos = list1.get(i);
int nextPos = (i + k1) % size1;
resArr[list1.get(nextPos)] = array[curPos];
C++ AC
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
const int maxn = 82;
char s1[maxn],s2[maxn];
int array1[maxn],array2[maxn],array3[maxn]; 
int main(){
    int i,size1,size2,size3;
    int k1,k2,k3;
    while(scanf("%d%d%d",&k1,&k2,&k3)!=EOF){
        if(k1 == 0 && k2 == 0 && k3 == 0){
            break;
        }
        scanf("%s1",s1);
        size1 = 0;
        size2 = 0;
        size3 = 0;
        int len = strlen(s1);
        for(i = 0; i < len; i++){
            if(s1[i] >= 'a' && s1[i] <= 'i'){
                array1[size1] = i;
                size1 ++;
            }else if(s1[i] >= 'j' && s1[i] <= 'r'){
                array2[size2] = i;
                size2 ++;
            }else if((s1[i] >= 's' && s1[i] <= 'z') || s1[i] == '_'){
                array3[size3] = i;
                size3 ++;
            }
        }
        for(i = 0; i < size1 ; i++){
            s2[array1[(i + k1) % size1]] = s1[array1[i]];
        }
        for(i = 0; i < size2 ; i++){
            s2[array2[(i + k2) % size2]] = s1[array2[i]];
        }
        for(i = 0; i < size3 ; i++){
            s2[array3[(i + k3) % size3]] = s1[array3[i]];
        }
        for(i = 0; i < len; i++){
            printf("%c",s2[i]);
        }
        printf("\n");
    }
    return 0;
}  
/**************************************************************
    Problem: 1485
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1020 kb
****************************************************************/

Java AC

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class Main {
    /*
     * 1485
     */
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int k1 = scanner.nextInt();
            int k2 = scanner.nextInt();
            int k3 = scanner.nextInt();
            if (k1 == 0 && k2 == 0 && k3 == 0) {
                break;
            }
            String a = scanner.next();
            char []array = a.toCharArray();
            int len = array.length;
            char []resArr = new char[len];
            List<Integer> list1 = new ArrayList<Integer>();
            List<Integer> list2 = new ArrayList<Integer>();
            List<Integer> list3 = new ArrayList<Integer>();
            for (int i = 0; i < len; i++) {
                if (array[i] >= 'a' && array[i] <= 'i') {
                    list1.add(i);
                }else if (array[i] >= 'j' && array[i] <= 'r') {
                    list2.add(i);
                }else if ((array[i] >= 's' && array[i] <= 'z' )|| array[i] == '_'){
                    list3.add(i);
                }
            }
            int size1 = list1.size();
            int size2 = list2.size();
            int size3 = list3.size();
            for (int i = 0; i < size1; i++) {
                int curPos = list1.get(i);
                int nextPos = (i + k1) % size1;
                resArr[list1.get(nextPos)] = array[curPos]; 
            }             
            for (int i = 0; i < size2; i++) {
                int curPos = list2.get(i);
                int nextPos = (i + k2) % size2;
                resArr[list2.get(nextPos)] = array[curPos]; 
            }             
            for (int i = 0; i < size3; i++) {
                int curPos = list3.get(i);
                int nextPos = (i + k3) % size3;
                resArr[list3.get(nextPos)] = array[curPos]; 
            }             
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < len; i++) {
                sb.append(resArr[i]);
            }
            System.out.println(sb);
        }
    }
}
/**************************************************************
    Problem: 1485
    User: wzqwsrf
    Language: Java
    Result: Accepted
    Time:100 ms
    Memory:17660 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值