ZOJ problem Set-1006 do the untwist

Source: Internet
Author: User
Tags arrays greatest common divisor
ZOJ problem Set-1006 Do the untwist time Limit:2 SECONDS          &N bsp;                           Memory limit:65536 kb                              

Cryptography deals with methods of secret communication that transform a message (the Plaintex T ) into a disguised form (the ciphertext ) so that no one seeing the ciphertext would be able TO&N Bsp 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, 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 is arrays of characters, and Plaincode and Ciphercode are  Arrays of integers.  All arrays was of length n, where n is the length of the message to being encrypted.  Arrays is Origin zero, so the elements is numbered from 0 to n-1. For this problem all messages would contain only lowercase letters, the period, and the underscore (representing a space).

The message to being 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 ' =, and '. ' = 27. Next, convert each code in Plaincode to a 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's x mod y is the positive remainder if x is divided by Y.)  For example, 3 mod 7 = 3, mod 8 = 6, and-1 mod 28 = 27. You can use the C ' percent ' 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 this can untwist messages, i.e., convert the ciphertext back to the original plaintext giv  En 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 th E file. Each test case was on a line by itself and consists of the key K, a space, and then a twisted message containing at least O  NE and at the most characters.  The key k would be is a positive integer not greater than 300. For each test case, the output of the untwisted message on a line by itself.

Note:you can assume that untwisting a message always yields a unique result. (For those of your with some knowledge of basic number theory or abstract algebra, this is the case provided the Greatest 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

AC code: My idea is to use the inverse of the array and map to query, fast, not the conversion over and over

#include <iostream>
#include <stdio.h>
#include <string>
#include <map>
using namespace Std;
int main ()
{
map<char,int>ccode;
Ccode[' _ ']=0;
ccode['. ' = 27;
for (int i=0;i<=25;i++)
{
ccode[' a ' +i]=i+1;
}
Char a[28];
A[0]= ' _ ';
A[27]= '. ';
for (int i=1;i<=26;i++)
{
a[i]=i+ ' a '-1;
}
int k;
while (cin>>k&&k!=0)
{
String Ctext;
cin>>ctext;
int Len=ctext.length ();
int *pcode=new Int[len];
for (int i=0;i<len;i++)
{
int T1=ccode[ctext[i]];
cout<< "T1:" <<t1<<endl;
t1= (t1+i)%28;
PCODE[K*I%LEN]=T1;
}
for (int i=0;i<len;i++)
{
cout<<a[pcode[i]];
}
cout<<endl;
}
}

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.