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:
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:
Example output:
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:
Array | 0 | 1 | 2 |
plaintext | 'c' | 'a' | 't' |
plaincode | 3 | 1 | 20 |
ciphercode | 3 | 19 | 27 |
ciphertext | 'c' | 's' | '.' |
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; }
相关文章推荐
- 最短路(最短路径)
- 递归与递推实现斐波那契数列算法
- 小小知识
- AngularJS中的模块(module)
- 简单的XSS攻击
- bash shell学习-正则表达式基础 (笔记)
- LINUX环境并发服务器的三种实现模型
- 微信 oauth授权 获取用户的信息
- 设计模式之Composite模式(笔记)
- cocos2d-x3.6 连连看连通画线
- 求出最长非降子序列的长度--LIS:longest increasing subsequence
- WebSocket 一场Web 通讯革命悄然来临——Node.js摸石头系列之五
- WebSocket 一场Web 通讯革命悄然来临——Node.js摸石头系列之五
- 新手安装及配置PHP开发环境
- 树莓派2+无线网卡=钓鱼热点
- MongDB入门简介
- Sort()函数是c++一
- 用php实现遍历目录
- 开始玩hadoop1--装java(ubuntu)
- UIView的属性方法解释(全)