poj 1026 Cipher

    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

Source

1.给出n个数字出现的顺序,给出一个字符串加密的次数,再给一串字符串,长度少于n,用空格补全,按着每个字符所处位置的数字进行排序加密,我们可以发现每个数字经过k次加密就能构成一个循环,这样我们只要找到这个循环周期k,问题就解决了,加密的次数用得到得循环次数k进行求余,这样才不会超时

2. lyar:说下题目大意。首先输入长度为n的数字串,设为key[i],表示第i个字符置换一次后跑到key[i]的位置。然后输入若干字符序列,设字符序列为Src(若长度不足n则后面用空格补齐),求Src中的每个字符进行m次置换后的字符序列Dst。

加密方式如题中所给:
1 2 3 4 5 6 7 8 9 10
4 5 3 7 2 8 1 6 10 9

对于第一个字符,加密3次的结果如下:

1 --> 4 --> 7 -->1

对于第二个字符,加密2次的结果如下:

2 --> 5 --> 2

可以看到,加密一定次数后,结果会构成一个循环,如果我们求出这个循环周期,那么加密的次数就可以使用取余运算进行缩减了。

这次没有使用C++来做,因为gets和puts很好用,而String在这里发挥不出什么作用...

3. 题意:有一种加密的方法,先给出 n 个数字的排列, 然后输入一个字符串,字符串长度 <= n。 当字符串长度小于n的时候,在其后面加上空格字符,使其长度等于n。

例如   4 5 3 7 2 8 1 6 10 9, Hello Bob, 然后从左到右让字符串的每一个字符与一个数字对应:

4      5      3      7      2      8      1      6      10      9

H     e       l       l       o      B      o      b      ‘ ’      ‘ ‘

假如加密后的到的新字符串用encode[]表示,那么加密过程就是encod[4] = H, encod[5] = e, encod[3] = l····。

连续加密K次,每一次在前一次得到的信息之上再加密。

题解:一开始直接模拟,然后判断整个字符串的循环节。TLE。然后想是不是可以先求出循环的K次幂,···没有下文。最后想到求单个字符的循环节,AC。

总结先用数字代替字母求周期,字母和数字一一对应

 

AC代码:

#include <stdio.h>
#include <string.h>
#define maxn 250
int a[maxn],t[maxn];
char s[maxn],str[maxn];
int n,m;
void work( int a[ ],int n)
{
int i,j;
int count;
for( i=1;i<=n;i++)
{
j=a[i];
count=1;
while(i!=j)
{
count++;
j=a[j];
}
t[i]=count;
}

}
int main( )
{
int i,j;
int len,num;
while(scanf("%d",&n)&&n)
{
for( i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
work(a,n);
while(scanf("%d",&m)&&m)
{
getchar( );
gets(s);

len=strlen(s);
for( i=len;i<n;i++)
{
s[i]=' ';
}
s[i]='\0';

for( i=1;i<=n;i++)
{
num=i;
for( j=0;j<m%t[i];j++)
{
num=a[num];
}

str[num-1]=s[i-1];
}
str[i-1]='\0';

puts(str);
}
printf("\n");

}
return 0;
}

链接:http://poj.org/problem?id=1026

posted @ 2012-07-24 17:49  jiai  Views(159)  Comments(0Edit  收藏  举报