Zju 1006 Zoj 1006

Source: Internet
Author: User
Tags arrays time limit
Do the untwist time limit:2 Seconds Memory limit:65536 KB

cryptography deals with methods of secret communication thattransform a message (the plaintext) into a D isguised form (theciphertext) so that no one seeing the ciphertext would be is ableto figure out the plaintext except The intended recipient. Transforming the plaintext to the ciphertext are encryption; transforming the ciphertext to the plaintext is de Cryption. twisting is a simple encryption method that requires that Thesender and recipient both agree on a secret key K, W Hich is apositive integer.

The twisting method uses four Arrays:plaintext and ciphertextare arrays of characters, and Plaincode and Ciphercode arear rays of integers. All arrays was of length n, where n is thelength of the message to being encrypted. Arrays is Origin zero, so theelements is numbered from 0 to n-1. For this problem allmessages would contain only lowercase letters, the period, and Theunderscore (representing a space).

The message to being encrypted is stored in plaintext. Given a Keyk, the encryption method works as follows. First convert theletters in plaintext to integer codes in Plaincode according tothe following rule: ' _ ' = 0, ' A ' = 1, ' B ' = 2, ..., ' z ' = 26,and '. ' = 27. Next, convert each of the code in Plaincode to anencrypted code in Ciphercode according to the following Formula:for Alli from 0 To N-1, ciphercode[i] = (plaincode[ki mod n]-i) MoD 28.

(here x mod y are the positive remainder when X was divided by y.for example, 3 mod 7 = 3, mod 8 = 6, and-1 mod 28 = 27. Youcan use the C ' percent ' operator or Pascal ' mod ' operator to computethis as long as you add y if the result is negative.) Finally, convert the codes in Ciphercode back to letters Inciphertext according to the rule listed above. The final twistedmessage is in ciphertext. Twisting the message cat using the key 5yields 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 this can untwist messages,i.e., convert the ciphertext back to the originalplaintext given The key K. For example, given the key 5 Andciphertext ' cs. ', your program must output the plaintext ' cat '.

The input file contains one or more test cases, followed by a linecontaining only the number 0 that signals the end of the File. Each test case was on a line by itself and consists of the key K, Aspace, and then a twisted message containing at least on E and at the most 70characters. The key k would be is a positive integer not greater than300. For each test case, the output of the untwisted message on a lineby itself.

Note:you can assume that untwisting a message always yieldsa unique result. (For those of your with some knowledge of basic numbertheory or abstract algebra, this is the case provided that Thegr Eatest common divisor of the key k and length n is 1, which it'll be-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


The main problem is how to find Plaincode, the formula in the problem can be Plaincode[ki mod n] = (ciphercode[i]+i) MoD 28, through the cycle of each to find out


#include <iostream>
#include <string>
using namespace Std;
Char plaincode[28];
int plain[1000];
int main () {
String Ciphertext,plaintext;
int key,n,ciphercode[1000];
Plaincode[0]= ' _ ';
Plaincode[27]= '. ';
for (int i=1;i<27;i++) {
plaincode[i]=i+ ' a '-1;
}
while (Cin>>key,key) {
cin>>ciphertext;
N=ciphertext.size ();
for (int i=0;i<n;i++) {
for (int j=0;j<28;j++) {
if (Plaincode[j]==ciphertext[i]) ciphercode[i]=j;
}
}
Plaintext= "";
for (int i=0;i<n;i++) {
plain[key*i%n]= (ciphercode[i]+i)%28;
}
for (int i=0;i<n;i++) {
Plaintext+=plaincode[plain[i]];
}
cout<<plaintext<<endl;
}
return 0;
}



Related Keywords:

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.