Do the Untwist(模拟)

ZOJ Problem Set - 1006
Do the Untwist

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Cryptography deals with methods of secret communication that transform a message (the plaintext) into a disguised form (the ciphertext) so that no one seeing the ciphertext will be able to figure out the plaintext except the intended recipient. Transforming the plaintext to the ciphertext is encryption; transforming the ciphertext to the plaintext is decryptionTwisting is a simple encryption method that requires that the sender and recipient both agree on a secret key k, which is a positive integer.

The twisting method uses four arrays: plaintext and ciphertext are arrays of characters, and plaincode and ciphercode are arrays of integers. All arrays are of length n, where n is the length of the message to be encrypted. Arrays are origin zero, so the elements are numbered from 0 to n - 1. For this problem all messages will contain only lowercase letters, the period, and the underscore (representing a space).

The message to be encrypted is stored in plaintext. Given a key k, the encryption method works as follows. First convert the letters inplaintext to integer codes in plaincode according to the following rule: '_' = 0, 'a' = 1, 'b' = 2, ..., 'z' = 26, and '.' = 27. Next, convert each code in plaincode to an encrypted code in ciphercode according to the following formula: for all i from 0 to n - 1,

ciphercode[i] = (plaincode[ki mod n- i) mod 28.

(Here x mod y is the positive remainder when x is divided by y. For example, 3 mod 7 = 3, 22 mod 8 = 6, and -1 mod 28 = 27. You can use the C '%' operator or Pascal 'mod' operator to compute this as long as you add y if the result is negative.) Finally, convert the codes in ciphercodeback to letters in ciphertext according to the rule listed above. The final twisted message is in ciphertext. Twisting the message cat using the key 5 yields the following:

 

Array 0 1 2
plaintext 'c' 'a' 't'
plaincode 3 1 20
ciphercode 3 19 27
ciphertext 'c' 's' '.'

Your task is to write a program that can untwist messages, i.e., convert the ciphertext back to the original plaintext given the key k. For example, given the key 5 and ciphertext 'cs.', your program must output the plaintext 'cat'.

The input file contains one or more test cases, followed by a line containing only the number 0 that signals the end of the file. Each test case is on a line by itself and consists of the key k, a space, and then a twisted message containing at least one and at most 70 characters. The key k will be a positive integer not greater than 300. For each test case, output the untwisted message on a line by itself.

Note: you can assume that untwisting a message always yields a unique result. (For those of you with some knowledge of basic number theory or abstract algebra, this will be the case provided that the greatest common divisor of the key k and length n is 1, which it will be for all test cases.)

Example input:

5 cs.
101 thqqxw.lui.qswer
3 b_ylxmhzjsys.virpbkr
0

Example output:

cat
this_is_a_secret
beware._dogs_barking

Source: Zhejiang University Local Contest 2001
 
一开始被“ki”蒙住了,以为i是下标来的。。。后来才醒悟是k*i。。。
思路:注意一点,ciphercode[i] = (plaincode[ki mod n] - i) mod 28 (记为1式) 一式中,(plaincode[ki mod n] - i)结果可能是负数。负数取余如何处理?题目中作了说明:如a mod b = c(a < 0, b > 0), 显然c < 0, 要得到正确结果,c要加上m*b,m的值满足0 <= m*b + c  < b
所以如果(plaincode[ki mod n] - i)是负数,那么得出的ciphercode[i]实际上是取余后的负数+28m而得,此时如果用ciphercode[i] 倒推plaincode[ki mod n],必然产生错误。
如果检测并纠正错误?
由题意,plaincode[ki mod n]<28,而如果(plaincode[ki mod n] - i)为负数,那么由ciphercode[i]倒推得到的plaincode[ki mod n]必然>=28,据此可以检错。不断减去28直到plaincode[ki mod n]<28成立为止即可纠错。
 
AC Code:
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int SZ = 71;
int k;
int pc[SZ];
char ct[SZ], pt[SZ];

int main()
{
    while(scanf("%d", &k) && k)
    {
        scanf("%s", ct);
        int n = strlen(ct);
        for(int i = 0; ct[i] != '\0'; i++)
        {
            int cc;
            switch(ct[i])
            {
            case '_':
                cc = 0;
                break;
            case '.':
                cc = 27;
                break;
            default:
                cc = ct[i] - 'a' + 1;
            }
            int j = (k * i) % n;
            pc[j] = cc + i;
            while(pc[j] > 27)
                pc[j] -= 28;
            if(pc[j] == 0)
                pt[j] = '_';
            else if(pc[j] == 27)
                pt[j] = '.';
            else
                pt[j] = (char)(pc[j] + 'a' - 1);

        }
        pt[n] = '\0';
        printf("%s\n", pt);
    }
    return 0;
}

 

posted on 2013-07-25 17:18  铁树银花  阅读(904)  评论(0编辑  收藏  举报

导航