软件工程

本类阅读TOP10

·PHP4 + MYSQL + APACHE 在 WIN 系统下的安装、配置
·Linux 入门常用命令(1)
·Linux 入门常用命令(2)
·使用 DCPROMO/FORCEREMOVAL 命令强制将 Active Directory 域控制器降级
·DirectShow学习(八): CBaseRender类及相应Pin类的源代码分析
·基于ICE方式SIP信令穿透Symmetric NAT技术研究
·Windows 2003网络负载均衡的实现
·一网打尽Win十四种系统故障解决方法
·数百种 Windows 软件的免费替代品列表
·收藏---行百里半九十

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
acm.jlu.edu.cn-1068-Cipher

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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; } }



相关文章

相关软件