Cipher (permutation group)

Source: Internet
Author: User
Tags eol cmath

Cipher
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20821 Accepted: 5708

Description

Bob and Alice started to use a brand-new encoding scheme. Surprisingly it isn't a public Key cryptosystem, but their encoding and decoding are based on secret keys. They chose the secret key at their last meeting with 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 it characters in the message and numbers in the key are correspondingly AL igned. Character in the message at the position I was written in the encoded message at the position AI, where AI is the Correspon Ding number in the key. And then the encoded message was encoded in the same. 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 was added to the end of the message to get the message with the length N.

Input

The input file consists of several blocks. Each block has a number 0 < n <=. 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 is 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 are 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 are one empty line.

Sample Input

104 5 3 7 2 8 1 6 CERC00 Hello Bob1995

Sample Output

Bolheol  BC RCE
Exercises
Give you a position, and then give you the string, asked to convert K-times after the string, violent, timed out ... Then is to use permutation group, find the Circulation section, mainly ans[b[(j+k)%num]]=s[b[j]];
Each position transforms (k%num) times
The explanation of the great God comes to me:
for permutation arrays (instead of using the next array), each element in it will exist within (and only) a permutation subgroup.
     For example, in the title data: (4, 7, 1, 4) (5, 2, 3, 3) (8, 6, 8) (9, 10).   you can find 4 subgroups.
Idea: Each subgroup has a certain number of different elements, using the variable T to record the number of subgroup elements, using son[] to store sub-group elements, there are son[0],son[1]...son[t-1] elements.
      Then we can get the following transform rule--New character array gain[son[(k + j)% T]] = original character array Str[son[j]] (0 <= J <= t-1).
            For example, the title data: 4-1 5-1 3-1 7-1 2-1 8-1 1-1 6-1 10-1 9-1 (for the corresponding string subscript decrement 1) hello+bob+ space with + representation
            Take Sub-group (3, 6, 0, 3 "has been reduced by one"), where t = 3,son[0] = 3, son[1] = 6, son[2] = 0.
            When k = 1 o'clock, {j = 0,son[(k + j)% t] = 6,gain[6] = str[3] = L
                         j = 1,son[(k + j)% t] = 0,gain[0] = str[6] = B
                         j = 2,son[(k + j)% t] = 3,gain[3] = str[0] = H
                       
Violence timeout:
#include <cstdio>#include<iostream>#include<cmath>#include<algorithm>#include<cstring>#include<queue>using namespacestd;Const intinf=0x3f3f3f3f;#defineMem (x, y) memset (x,y,sizeof (x))#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)typedefLong LongLL;Const intmaxn= About;intPOS[MAXN];intMain () {intN;  while(~SCANF ("%d",&N), N) {intK; CharS[MAXN],ANS[MAXN];  for(intI=0; i<n;i++) SI (Pos[i]);  while(SCANF ("%d%*c",&k), k) {mem (s),0);            Gets (s);  while(k--){                 for(intI=0; i<n;i++){                if(I>=strlen (s)) ans[pos[i]-1]=' '; Elseans[pos[i]-1]=S[i]; } Ans[n]=' /';            strcpy (S,ans);        } puts (ans); }    }    return 0;}

Permutation group:

#include <cstdio>#include<iostream>#include<cmath>#include<algorithm>#include<cstring>#include<queue>using namespacestd;Const intinf=0x3f3f3f3f;#defineMem (x, y) memset (x,y,sizeof (x))#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)typedefLong LongLL;Const intmaxn= About;intNEXT[MAXN];intVIS[MAXN];CharANS[MAXN];intB[MAXN];intMain () {intN;  while(~SCANF ("%d",&N), N) {intK; CharS[MAXN];  for(intI=0; i<n;i++) SI (Next[i]), next[i]--;  while(SCANF ("%d%*c",&k), K) {gets (s); Mem (Vis,0);  for(intI=strlen (s); i<n;i++) s[i]=' ';  for(intI=0; i<n;i++){                if(!Vis[i]) {                    intj=i,num=0;  while(!Vis[j]) {Vis[j]=1; B[num++]=J; J=Next[j]; }                     for(j=0; j<num;j++) {ans[b[(J+K)%num]]=S[b[j]]; }}} Ans[n]=' /';        Puts (ans); } puts (""); }    return 0;}

Cipher (permutation group)

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.