[POJ 1026]Cipher

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

10

4 5 3 7 2 8 1 6 10 9

1 Hello Bob

1995 CERC

0

0

Sample Output

BolHeol b

C RCE

题目大意:

给你一个置换,在给你很多字符串,对于每一个字符串,做m次置换,输出做完m次置换之后的字符串。

题解:

直接模拟肯定是会超时的,那么我们对于每一个位置,求出它所在的循环的长度len,那么实际上就只需要向后找m%len步就好了。

 1 //Never forget why you start
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 using namespace std;
 9 int n,m,a[205],sum[205],cnt,vis[205],b[205];
10 char s[205];
11 void dfs(int r){
12   vis[r]=1;
13   cnt++;
14   if(vis[a[r]]){sum[r]=cnt;return;}
15   else {dfs(a[r]);sum[r]=cnt;}
16 }
17 int main(){
18   int i,j;
19   while(scanf("%d",&n)!=EOF){
20     if(n==0)break;
21     memset(vis,0,sizeof(vis));
22     for(i=1;i<=n;i++)scanf("%d",&b[i]),a[b[i]]=i;
23     for(i=1;i<=n;i++)
24       if(!vis[i]){
25     cnt=0;
26     dfs(i);
27       }
28     while(scanf("%d",&m)!=EOF){
29       if(m==0)break;
30       getchar();
31       cin.getline(s,sizeof(s));
32       int len=strlen(s);
33       for(i=len;i<n;i++)s[i]=' ';
34       for(i=0;i<n;i++){
35     j=i+1;
36     cnt=m%sum[j];
37     while(cnt--)j=a[j];
38     printf("%c",s[j-1]);
39       }
40       printf("\n");
41     }
42     printf("\n");
43   }
44   return 0;
45 }

 

posted @ 2018-01-23 22:00  kakakakakaka  阅读(270)  评论(0编辑  收藏  举报

Never forget why you start

//鼠标爆炸特效