POJ:Cipher

 

Cipher
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13623 Accepted: 3434

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
原题地址:http://poj.org/problem?id=1026
____________________________________________________________________________________________________
DISCUSS里说要用置换群,百度了一下对置换群还是不太了解。
仔细研究一下样例,发现每个字母经过一定次数变换后一定会回到原来的位置,且这个变换次数肯定不会大于N。
这样就简单了,先模拟做N次变换,记录下每个字母变换i次后的位置TRANS[I][J],再记录下每个字母经过多少次变换会回到一开始的位置,记为M[j]次
对于每个输入,答案就是trans[k mod m[i]][J];


代码
1 #include<stdio.h>
2 #include<string.h>
3  int trans[205][205],mod[205],key[205];
4  char str[205],ans[205];
5  int i,j,n,k,temp;
6 int main()
7 {
8 while (scanf("%d",&n)!=EOF)
9 {
10 if (n==0) break;
11 for (i=1;i<=n;i++)
12 {
13 scanf("%d",&key[i]);
14 trans[0][i]=i;
15 }
16 for (i=1;i<=n;i++)
17 for (j=1;j<=n;j++)
18 {
19 trans[i][j]=key[trans[i-1][j]];
20 if (trans[i][j]==trans[0][j])
21 mod[j]=i;
22 }
23 while (scanf("%d",&k)!=EOF)
24 {
25 if (k==0) break;
26 getchar();
27 gets(str);
28 if (strlen(str)<n)
29 for (i=strlen(str);i<n;i++)
30 str[i]=' ';
31 str[n]='\0';
32 for (i=0;i<n;i++)
33 {
34 temp=k%mod[i+1];
35 ans[trans[temp][i+1]-1]=str[i];
36 }
37 ans[n]='\0';
38 printf("%s\n",ans);
39 }
40 }
41 }
42

 


posted on 2011-01-24 15:35  风也轻云也淡  阅读(256)  评论(0编辑  收藏  举报