主题:密码题,但又超时了.求救!
这次的题目是PKU--Cipher:我的做法是按照题目要求逐步求解,因为好像用不着什么技巧,我也想不出什么技巧.但提交时就是超时.不知何解!故在此诚心请教各位高手.谢谢!麻烦了!
下面是原题和我的代码:
Cipher
Time Limit:1000MS Memory Limit:10000K
Total Submit:3835 Accepted:787
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
Central Europe 1995
我的代码:
#include <stdio.h>
#include <string.h>
void main()
{
int i,j,n,flag;
int code[202];
long k;
char ch,c[202],hello[202];
while(1)
{
scanf("%d",&n);//编码长度
if(!n)
break;
for(i=1;i<=n;i++)//输入数字编码
scanf("%d",&code[i]);
while(1)
{
scanf("%d",&k);//循环编码次数k
getchar();//读取回车符
if(!k)
break;
memset(c,' ',202);//预先将字符数组设置为空格
memset(hello,' ',202);//预先将字符数组设置为空格
for(i=1;i<=n;i++)//输入字符数组
{
ch=getchar();
if(ch=='\n')
break;
c[i]=ch;
}
for(i=1;i<=n;i++)//对字符数组中每个元素进行运算,确定其最终位置
{
flag=i;//第flag的字符的首位置是i
for(j=1;j<=k;j++)//经过k次编码,最终位置是flag
flag=code[flag];
hello[flag]=c[i];//在字符数组hello中,在位置flag赋值为c[i],即第i个字符
}
for(i=1;i<=n;i++)//输出
printf("%c",hello[i]);
printf("\n");
}
printf("\n");
}
}
再次感谢您的帮助!
下面是原题和我的代码:
Cipher
Time Limit:1000MS Memory Limit:10000K
Total Submit:3835 Accepted:787
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
Central Europe 1995
我的代码:
#include <stdio.h>
#include <string.h>
void main()
{
int i,j,n,flag;
int code[202];
long k;
char ch,c[202],hello[202];
while(1)
{
scanf("%d",&n);//编码长度
if(!n)
break;
for(i=1;i<=n;i++)//输入数字编码
scanf("%d",&code[i]);
while(1)
{
scanf("%d",&k);//循环编码次数k
getchar();//读取回车符
if(!k)
break;
memset(c,' ',202);//预先将字符数组设置为空格
memset(hello,' ',202);//预先将字符数组设置为空格
for(i=1;i<=n;i++)//输入字符数组
{
ch=getchar();
if(ch=='\n')
break;
c[i]=ch;
}
for(i=1;i<=n;i++)//对字符数组中每个元素进行运算,确定其最终位置
{
flag=i;//第flag的字符的首位置是i
for(j=1;j<=k;j++)//经过k次编码,最终位置是flag
flag=code[flag];
hello[flag]=c[i];//在字符数组hello中,在位置flag赋值为c[i],即第i个字符
}
for(i=1;i<=n;i++)//输出
printf("%c",hello[i]);
printf("\n");
}
printf("\n");
}
}
再次感谢您的帮助!