Do the untwist

Source: Internet
Author: User
Do the untwist Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/others) total submission (s): 982 accepted submission (s): 638Problem descriptioncryptography 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 before t 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 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 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' '.'
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 .) sample Input
5 cs.101 thqqxw.lui.qswer3 b_ylxmhzjsys.virpbkr0
Sample output
Catthis_is_a_secretbeware. _ dogs_barking

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1129

The question is very simple, and the plain text is reversed. The example of the question is: [cat --> CS .]
Step 1: Convert cat to the corresponding number plaincode. As mentioned in this article, _ = 0,. = 27, A = 1... [cat --> 3, 1, 20]
Step 2: Use the formula to convert plaincode into the number ciphercode [3, 1, 20 --> 3, 19, 27]
Step 3: Convert ciphercode into a ciphertext [3, 19, 27 --> CS .]
The key K and ciphertext are given in the question, and the plaintext must be obtained. The most important thing about this question is the conversion of formulas!
The formula is as follows:
Plaintext --> ciphertext: ciphercode [I] = (plaincode [Ki mod n]-I) mod 28.
Ciphertext --> plaintext: plaincode [Ki mod n] = (ciphercode [I] + I) mod 28.

 1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5     char s[100],b[100]; 6     int i,j,n,k; 7     while(scanf("%d",&k),k) 8     { 9         scanf("%s",s);10         n=strlen(s);11         for(i=0;i<n;i++)12         {13             if(s[i]==‘_‘)14                 s[i]=0;15             else if(s[i]==‘.‘)16                 s[i]=27;17             else18                 s[i]=s[i]-‘a‘+1;                19         } 20         for(i=0;i<n;i++)21         {22             b[k*i%n]=(s[i]+i)%28;23         }24         for(i=0;i<n;i++)25         {26             if(b[i]==27)27                 printf(".");28             else if(b[i]==0)29                 printf("_");30             else31                 printf("%c",b[i]+‘a‘-1);32         }33         printf("\n");    34     }35     return 0;36 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.