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
10 4 5 3 7 2 8 1 6 10 9 1 Hello Bob 1995 CERC 0 0
Sample Output
BolHeol b C RCE
#include<iostream> #include<string> #include<memory> using namespace std; const int N=201; void main() { int key[N]; //secret kay int keys[N]; //group information char ct[N]; //CipherText bool used[N]; int cycle[N][N]; int n; while(cin>>n,n>0) { for(int i=0;i<n;i++) { cin>>key[i]; } memset(used,0,n*sizeof(bool)); int x=0; int idxc=0; while(x<n) { if(used[x]) { x++; continue; } else { used[x]=true; int mark=x; int len=1; cycle[idxc][len]=key[x]; while(key[x]!=mark+1) { len++; x=key[x]-1; used[x]=true; cycle[idxc][len]=key[x]; } cycle[idxc][0]=len; for(int i=1;i<=len;i++) { keys[cycle[idxc][i]-1]=idxc; } x=mark; idxc++; } x++; } int m; while(cin>>m,m>0) { char ch; cin.get(ch); int idx=0; while(cin.get(ch),ch!='/n') { ct[idx++]=ch; } for(int i=idx;i<n;i++) ct[i]=' '; char it[N];//intertext; memset(used,0,n*sizeof(bool)); x=0; while(x<n) { if(used[x]) { x++; continue; } int mark=keys[x]; int step=m%cycle[mark][0]; int pi=1,pj=pi+step; for(int i=0;i<cycle[mark][0];i++) { used[cycle[mark][pj]-1]=true; it[cycle[mark][pj]-1]=ct[cycle[mark][pi]-1]; pi++;pj++; if(pj==cycle[mark][0]+1) pj=1; } x++; } it[n]='/0'; cout<<it<<endl; } cout<<endl; } }
Bob和Alice的编码方案程序实现
Bob和Alice采用基于秘密密钥的编码方案,密钥是n个不同整数序列。编码时将消息字符按密钥对应位置重排,重复k次。消息长度小于n时补空格。需编写程序读取密钥及k和消息对,输出编码后的消息。
1968

被折叠的 条评论
为什么被折叠?



