ZOJ1006解题报告

来源:互联网 发布:socket accept 端口 编辑:程序博客网 时间:2024/05/29 08:51
 
Do the Untwist

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Cryptography deals with methods of secret communication that transform a message (theplaintext) 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 isencryption; transforming the ciphertext to the plaintext isdecryption. Twisting is a simple encryption method that requires that the sender and recipient both agree on a secret keyk, which is a positive integer.

The twisting method uses four arrays: plaintext and ciphertext are arrays of characters, andplaincode and ciphercode are arrays of integers. All arrays are of lengthn, where n is the length of the message to be encrypted. Arrays are origin zero, so the elements are numbered from 0 ton - 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 inplaincode to an encrypted code in ciphercode according to the following formula: for alli from 0 to n - 1,

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

(Here x mod y is the positive remainder when x is divided byy. 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 addy if the result is negative.) Finally, convert the codes in ciphercode back to letters inciphertext according to the rule listed above. The final twisted message is inciphertext. Twisting the message cat using the key 5 yields the following:

Array012plaintext'c''a''t'plaincode3120ciphercode31927ciphertext'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 keyk. 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 keyk, a space, and then a twisted message containing at least one and at most 70 characters. The keyk 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 keyk and length n is 1, which it will be for all test cases.)

Example input:

5 cs.101 thqqxw.lui.qswer3 b_ylxmhzjsys.virpbkr0

Example output:

catthis_is_a_secretbeware._dogs_barking

这道题是一道简单的模拟题,即告诉你加密的方法,然后输入是加密后的密文,希望程序运行后,输出解密后的明文。

那么,解密过程实际上是加密过程的逆向过程。

首先,令plainText为明文字符串数组,cipherText为密文字符串数组,plainCode为明文数字数组,cipherCode为密文数字数组。

1.输入密文,放入cipherText中,len=strlen(cipherText)

2.根据'a'~'z'为1~26,'_'为0,'.'为27,将cipherText转变为数字放入cipherCode中。

3.plainCode[(i*k)%len]=(cipherCode[i]+i)%28

4.将plainCode再按上述规则转变为明文数组plainText

5.输出plainText

代码如下:

#include<stdio.h>#include<string.h>//明文字符串char  plainText[70],cipherText[70];int plainCode[70],cipherCode[70];int k,len;int main(){   int i;   while(1)   {      scanf("%d",&k);      if(k==0)      {          break;       }       scanf("%s",cipherText);            len=strlen(cipherText);            for(i=0;i<len;i++)      {         if(cipherText[i]>='a'&&cipherText[i]<='z')         {             cipherCode[i]=cipherText[i]-'a'+1;          }          else if(cipherText[i]=='_')         {             cipherCode[i]=0;         }         else if(cipherText[i]=='.')         {             cipherCode[i]=27;         }      }      for(i=0;i<len;i++)      {         plainCode[(i*k)%len]=(cipherCode[i]+i)%28;       }      for(i=0;i<len;i++)      {         if(plainCode[i]>=1&&plainCode[i]<=26)         {            plainText[i]='a'-1+plainCode[i];          }          else if(plainCode[i]==0)         {            plainText[i]='_';          }         else if(plainCode[i]==27)         {            plainText[i]='.';          }      }      for(i=0;i<len;i++)      {         printf("%c",plainText[i]);       }      printf("\n");   }   system("pause");   return 0; }


 

原创粉丝点击