POJ1026 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次

对循环长度取模,然后模拟变换就行了

强调一下,代码中之所以是a[x]=i

举例说明:7->1实际上的意思是7位置上的数移到1位置

实际上变换后1的值变成了7的值,所以不是a[7]=1而是a[1]=7

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

 

posted @ 2018-02-05 14:13  Z-Y-Y-S  阅读(256)  评论(0编辑  收藏  举报