您的位置:首页 > 其它

ZOJ Problem Set - 1006 Do the Untwist

2015-06-27 15:18 513 查看
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 decryption.Twisting 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 in plaintext to integer
codes in plaincodeaccording 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 ciphercode back 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:

Array012
plaintext'c''a''t'
plaincode3120
ciphercode31927
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


思路分析:

思路简单,无非就是对加密后的信息解密求出原始信息

加密过程:
plaintext-->plaincode-->cyphercode-->cyphertext

ciphercode[i]=(plaincode[(k*i)%n]-i)mod 28(题目已知)

解密过程就是加密的逆过程
cyphertext-->cyphercode-->plaincode-->plaintext
plaincode[(i*k)%n]=(cyphercode[i]+i)%28   (关键解密公式)

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

int k;
int n;
char ctext[100];//加密后的信息
int ccode[100];//加密后信息的数字表示
char ptext[100];//未加密的原始信息
int pcode[100];//未加密信息的数字表示
int main(){
	int i;
	while (scanf("%d", &k) && k){
		i = 0;
		scanf("%s", ctext);
		
		n = strlen(ctext);
		for (i = 0; i < n; i++){
			switch (ctext[i]){
			case '.':ccode[i] = 27; break;
			case '_':ccode[i] = 0; break;
			default:ccode[i] = ctext[i] - 'a' + 1; break;
			}
		}//将cyphertext转换成cyphercode
		for (i = 0; i < n; i++){
			pcode[(i*k) % n] = (ccode[i] + i) % 28;
		}//将cyphercode转换成plaincode
		for (i = 0; i < n; i++){
			switch (pcode[i]){
			case 27:ptext[i] = '.'; break;
			case 0:ptext[i] = '_'; break;
			default:ptext[i] = 'a' + pcode[i] - 1; break;
			}
		}//将plaincode转换成plaintext
		printf("%s\n", ptext);
		memset(ccode, 0, n);
		memset(ctext, '\0',n);
		memset(pcode, 0, n);
		memset(ptext, '\0', n);
               //重新归为0或者‘\0'否则可能会影响下一次信息的解密过程
	}
	return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: