题目链接 : POJ1026
Cipher
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17849 | Accepted: 4712 |
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
还是博弈 想起那段时间 做了好多博弈的说~
以上口胡 看到Bob,Alice就想起博弈。。
这题是个置换群的题
有两种写法
一种可以仿照快速幂的思想 O(n*logk)
另一种怎么说呢。。以后补~ 复杂度是 O(n)
快速置换群
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
int num[205],p[205],c[205],temp[205]; //置换在num中
char str[205],ans[205];
int n;
void change(int a[],int b[])
{
for(int i=1;i<=n;i++)
temp[i]=b[i];
for(int i=1;i<=n;i++)
a[i]=temp[a[i]];
}
void pow_group(int k)
{
for(int i=1;i<=n;i++)
{
p[i]=num[i];
c[i]=i;
}
while(k)
{
if(k&1) change(c,p);
k>>=1;
change(p,p);
}
}
int main()
{
while(scanf("%d",&n)==1)
{
if(n==0) break;
for(int i=0;i<n;i++)
scanf("%d",num+i+1);
int k=0;
while(scanf("%d",&k))
{
if(k==0) break;
pow_group(k);
gets(str);
int i;
for(i=1;str[i];i++);
for(;i<=n;i++) str[i]=' ';
for(i=1;i<=n;i++) ans[c[i]-1]=str[i];
ans[n]=0;
for(i=0;i<n;i++)
putchar(ans[i]);
putchar(10);
}
putchar(10);
}
return 0;
}
gcd方法(虽然没用gcd)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
bool vis[205];
int num[205],nex[205],c[205];
char ans[205],str[205];
int n;
void pow_group(int k)
{
int sta=1,len=1;
for(int i=1;i<=n;i++)
{
if(sta==num[nex[i]])
{
for(int j=0;j<len;j++)
c[nex[i-len+1+j]]=nex[i-len+1+(j+k)%len];
len=1;
sta=nex[i+1];
}
else len++;
}
}
int main()
{
while(scanf("%d",&n)==1)
{
if(n==0) break;
for(int i=1;i<=n;i++)
scanf("%d",num+i),vis[i]=0;
int k=1,sta,now;
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
sta=i;
vis[i]=1;
nex[k++]=i;
now=num[i];
while(sta!=now)
{
vis[now]=1;
nex[k++]=now;
now=num[now];
}
}
int z=0;
while(scanf("%d",&z))
{
if(z==0) break;
gets(str);
pow_group(z);
int i;
for(i=1;str[i];i++);
for(;i<=n;i++) str[i]=' ';
for(i=1;i<=n;i++) ans[c[i]-1]=str[i];
ans[n]=0;
for(i=0;i<n;i++)
putchar(ans[i]);
putchar(10);
}
putchar(10);
}
return 0;
}