Poj 1026 cipher (replacement)

Source: Internet
Author: User

Cipher

Time limit:1000 ms   Memory limit:10000 K
Total submissions:19502   Accepted:5239

Description

Bob and Alice started to use a brand-new encoding scheme. surprisingly it is not a public key cryptosystem, but their encoding and decoding is based on secret keys. they chose the secret key at their last meeting in Philadelphia on February 16th, 1996. they chose as a secret key a sequence of N distinct integers, A1 ;...; an, greater than zero and less or equal to n. the encoding is based on the following principle. the message is written down below the key, so that characters in the message and numbers in the key are correspondingly aligned. character in the message at the position I is written in the encoded message at the position AI, where Ai is the corresponding number in the key. and then the encoded message is encoded in the same way. this process is repeated K times. after kth encoding they exchange their message.

The length of the message is always less or equal than N. if the message is shorter than N, then spaces are added to the end of the message to get the message with the length N.

Help Alice and Bob and write program which reads the key and then a sequence of pairs consisting of K and message to be encoded K times and produces a list of encoded messages.

Input

The input file consists of several blocks. each block has a number 0 <n <= 200 in the first line. the next line contains a sequence of N numbers pairwise distinct and each greater than zero and less or equal than N. next lines contain integer number K and one message of ASCII characters separated by one space. the lines are ended with EOL, this EOL does not belong to the message. the block ends with the separate line with the number 0. after the last block there is in separate line the number 0.

Output

Output is divided into blocks corresponding to the input blocks. each block contains the encoded input messages in the same order as in input file. each encoded message in the output file has the lenght n. after each block there is one empty line.

Sample Input

104 5 3 7 2 8 1 6 10 91 Hello Bob1995 CERC00

Sample output

BolHeol  bC RCE

For the first time, let's record the questions that are solved separately for the elements in each loop section.


Given a replacement of N numbers, replace a string K times according to the replacement rules. If the length of a string is less than N, fill in spaces at the end of the string until the length is N. What is the string after K replacement.


Analysis: If you directly follow the simulation described in the question, it will definitely time out.
Replacement of the entire string can be converted to replacement of each cyclic section. Because the elements in a circular section have no influence on other elements, we can first find all the elements in the circular section and the length of the circular section in each circular section, then, replace the elements in each cyclic section. An element in a circular section is replaced by K times, which is equivalent to K % (the length of the circular section where the element is located) times. In this way, the number of simulation times becomes small and can be simulated.


# Include <cstdio> # include <cstring> # include <string> # include <algorithm> # include <iostream> using namespace STD; const int n = 215; char STR [N]; // The original string char ans [N]; // The replaced string int key [N]; // The replacement rule int CNT; // Number of cyclic nodes int num [N]; // The length of each cyclic node int CIR [N] [N]; // CIR [I] [J] indicates the position of the J-th element in the I-th loop section, int vis [N]; // records whether each number has accessed int n; void get_circle () {memset (VIS, 0, sizeof (VIS); memset (Num, 0, sizeof (Num); CNT = 0; For (INT I = 1; I <= N; I ++) {If (! Vis [I]) {vis [I] = 1; num [CNT] = 0; int TMP = Key [I]; CIR [CNT] [num [CNT] ++] = TMP; while (! Vis [TMP]) {vis [TMP] = 1; TMP = Key [TMP]; CIR [CNT] [num [CNT] ++] = TMP ;} CNT ++ ;}}int main () {int K; while (~ Scanf ("% d", & N) {for (INT I = 1; I <= N; I ++) scanf ("% d ", & Key [I]); get_circle (); While (~ Scanf ("% d", & K) {gets (STR); int Len = strlen (STR); For (INT I = Len; I <= N; I ++) STR [I] = ''; For (INT I = 0; I <CNT; I ++) {for (Int J = 0; j <num [I]; j ++) {ans [CIR [I] [(j + k) % num [I] = STR [CIR [I] [J];} // The position after K replacement of element J in cyclic section I is (J + k) % num [I]} ans [n + 1] = '\ 0'; printf ("% s \ n", ANS + 1 );} printf ("\ n");} return 0 ;}



Poj 1026 cipher (replacement)

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.