poj1026 Cipher (循环节)

Cipher
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22140 Accepted: 6123

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


题意:根据所给的位置映射关系,将一个字符串加密。

题解:每个字母在若干次变化后肯定会回到初始位置,那么就可就可以求出循环节。一开始我用了并查集,将这些在一个循环节里的数字归为一组然后放在一个set里面,每次访问,就让加密次数对set的size取余。但是这么做超时了,如果set只有一个,那么取余后最大是200,那么对于这个极限数据复杂度就是n方了,虽然n最大是200,但是数据有很多组。既然n最大是200,那么其实就可以通过对每个位置变换i次,将每次的位置记录下来,那么对于访问就降到了O(1),记录的操作也不过O(n)。这是一个很大的优化,这题虽然一开始有了正确的思路,但是代码的实现却暴力了,由于我在求取余位置时,是循环找儿子节点(不过如果把超时代码中的set换成是vector,那么在求取余后的位置就可以不用for了,其实这个vector就是那个move的二维数组了)。

我的超时代码:

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<bitset>

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>

#include<iomanip>
#include<iostream>

#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;

const int N = 100000 + 5;
const int mod = 1000000000 + 7;
int a[N],F[N],n,num,k,pre,fa,son;
char str[N],ans[N];
bool vis[N];
set<int> s[N];
set<int>::iterator it;

int Find(int x){
	return (x==F[x])?x:F[x]=Find(F[x]);
}

int main(){
	while(~scanf("%d",&n)&&n){
		for(int i=1;i<=n;i++){
			F[i]=i;
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(Find(a[i])!=Find(i)){
				F[a[i]]=Find(i);
			}
			s[Find(i)].insert(a[i]);
		}
		while(~scanf("%d",&num)&&num){
			mem(vis,0),mem(ans,0),mem(str,0);
			getchar();
			cin.getline(str+1,N);
			for(int i=1;i<=n;i++){
				if(!vis[i]){
					fa=Find(i);
					k=num%s[fa].size();
					for(it=s[fa].begin();it!=s[fa].end();it++){
						son=(*it);
						vis[son]=1;
						pre=son;
						for(int j=1;j<=k;j++){
							pre=a[pre];
						}
						ans[pre]=str[son];
					}
				}
			}
			for(int i=1;i<=n;i++){
				if(!ans[i]){
					printf(" ");
				}
				else{
					printf("%c",ans[i]);
				}
			}
			puts("");
		}
	}
	return 0;
}

ac代码:

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<bitset>

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>

#include<iomanip>
#include<iostream>

#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;

const int N = 200 + 5;
const int mod = 1000000000 + 7;
char str[N],ans[N];
int a[N],cnt,q,n;//cnt是置换环个数 
int move[N][N];//表示位置i经过j次转移后的位置
int num[N];//置换环长度
bool vis[N]; 

int main(){
	while(~scanf("%d",&n)&&n){
		mem(move,0),mem(num,0),mem(vis,0),cnt=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		for(int i=1;i<=n;i++){//找循环节
			if(!vis[i]){
				int kk=a[i];
				vis[i]=1,num[cnt]=0;
				move[cnt][num[cnt]++]=a[i];
				while(!vis[kk]){
					vis[kk]=1;
					kk=a[kk];
					move[cnt][num[cnt]++]=kk;
				}
				cnt++;
			}
		}
		while(~scanf("%d",&q)&&q){
			mem(str,0),mem(ans,0),mem(vis,0);
			gets(str);
			for(int i=strlen(str);i<=n;i++){
				str[i]=' ';
			}
			str[n+1]='\0';
			for(int i=0;i<cnt;i++){
				for(int j=0;j<num[i];j++){
					ans[move[i][(j+q)%num[i]]]=str[move[i][j]];
				}
			}
			ans[n+1]='\0';
			printf("%s\n",ans+1);
		}
		puts("");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值