Cipher
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 20910 | Accepted: 5731 |
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.
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
题意:给出一个长度为n的序列a,a[i]表示原字符串中第i个字母在一次变换后放在新字符串中的第a[i]个位置。
让你求出k次变换后的字符串
思路:求出每个字符所在组的大小,该大小既是它的变换周期,对这个位置进行k次变换等价于对其进行k%周期次变换。
先预处理出每组内的大小,并且用val数组构建出每个位置的下个位置。 对于每个字符串再通过已求出的周期和变换关系进行变换得出最后结果。
注意:这个题目格式异常坑爹,在格式上wa了很多遍,注意给出的每个字符串前都有空格。
代码
#include <stdio.h> #include <string.h> #include <iostream> #include <functional> #include <queue> #include <vector> using namespace std; int main(int argc, char const *argv[]) { int a[220],b[220],vis[220],val[220],sum[220]; int i,j,k,m,n,t; while(~scanf("%d",&n)) { if(n==0)break; for(i=0;i<=n;i++)b[i]=i; memset(a,0,sizeof(a)); memset(val,0,sizeof(val)); memset(vis,0,sizeof(vis)); memset(sum,0,sizeof(sum)); for(i=0;i<n;i++){scanf("%d",&a[i]);a[i]--;val[a[i]]=i;} for(i=0;i<n;i++) if(!vis[a[i]]) { int be=a[i]; int x=a[i]; vis[x]=1; int cnt=1; while(val[x]!=be) { cnt++; x=val[x]; vis[x]=1; } sum[a[i]]=cnt; x=a[i]; while(val[x]!=be) { x=val[x];sum[x]=cnt; } } //for(i=0;i<n;i++)printf("%d ",sum[a[i]]); //printf("\n"); while(~scanf("%d",&k)) { char str[220],s[220]; if(k==0)break; for(i=0;i<n;i++)str[i]=' '; str[n]='\0'; getchar(); gets(s); for(i=strlen(s);i<n;i++)s[i]=' '; s[n]='\0'; int l=strlen(s); for(i=0;i<n;i++) {int k1=k%sum[a[i]]; int x=i; for(j=1;j<=k1;j++)x=val[x]; str[i]=s[x]; } printf("%s\n",str); } printf("\n"); } return 0; }

这篇博客详细解析了ACM竞赛中的POJ1026题目,主要涉及置换群的概念和多次置换操作。通过对输入输出样例的分析,阐述了解题思路和解决方案。

6831

被折叠的 条评论
为什么被折叠?



