UVa 306 / POJ 1026 / Central Europe 1995 Cipher (置换群)

262 篇文章 0 订阅
119 篇文章 0 订阅

306 - Cipher

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=242

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

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, tex2html_wrap_inline27 , greater than zero and less or equal ton. 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 positioni is written in the encoded message at the position tex2html_wrap_inline33 , wheretex2html_wrap_inline33 is the corresponding number in the key. And then the encoded message is encoded in the same way. This process is repeatedk times. Afterkth encoding they exchange their message.

The length of the message is always less or equal than n. If the message is shorter thann, then spaces are added to the end of the message to get the message with the lengthn.

Help Alice and Bob and write program which reads the key and then a sequence of pairs consisting ofk and message to be encodedk times and produces a list of encoded messages.

Input

The input file consists of several blocks. Each block has a number tex2html_wrap_inline51 in the first line. The next line contains a sequence ofn numbers pairwise distinct and each greater than zero and less or equal thann. Next lines contain integer numberk 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 lenghtn. 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     


“Character in the message at the position i is written in the encoded message at the positiontex2html_wrap_inline33 , wheretex2html_wrap_inline33 is the corresponding number in the key.”

加密过程如下:(以原文的第7个字符ch为例)

在样例中,ch被写在一次加密的密文中的第a7处(即第1个位置),进而可推知ch被写在二次加密的密文中的第a(a7)处(即第4个位置),三次加密的密文中的第a(a(a7))处(即第7个位置)。简记为a^1(7)=1,a^2(7)=4,a^3(7)=7。

若把a^0(7)=7也算在内,我们似乎发现了一些周期性的规律:a^(m%3)(7)=7,1,4 (m%3=0,1,2)

据此,把序列{an}写成不交轮换的合成的形式:(4 7 1)(5 2)(3)(8 6)(10 9) 

这样,原文的第i个字符在k次加密的密文中,应被写在第a^(k%Ti)(i)处,其中Ti指i所在的轮换的长度(也可以称作最小正周期),具体的计算细节见下方代码。


注意:不要用"%d "读掉中间空格,因为可能原文的第一个字符就是空格。


复杂度:O(n)

完整代码:

/*UVa: 0.049s*/
/*POJ: 94ms,180KB*/

#include<cstdio>
#include<cstring>
#include<vector>
#include<utility>
using namespace std;
const int maxn = 205;

char s[maxn], ans[maxn];
int a[maxn];
bool vis[maxn];
pair<int, int> belong[maxn];
vector<int> group[maxn], tmp;

int main()
{
	int n, i, j, cnt, k;
	while (scanf("%d", &n), n)
	{
		for (i = 1; i <= n; ++i)
		{
			scanf("%d", &k);
			a[i] = k;
		}
		memset(vis, 0, sizeof(vis));
		for (i = 0; i < n; ++i) group[i].clear();
		cnt = 0;
		for (i = 1; i <= n; ++i)
		{
			if (!vis[i])
			{
				for (j = a[i]; !vis[j]; j = a[j])
				{
					belong[j] = make_pair(cnt, group[cnt].size());
					group[cnt].push_back(j);///构造轮换
					vis[j] = true;
				}
				++cnt;
			}
		}
		///prepare
		while (scanf("%d", &k), k)
		{
			getchar();///为什么上面不写"%d "来读掉中间的空格?因为可能原文的第一个字符就是空格orz...
			gets(s + 1);
			for (i = strlen(s + 1) + 1; i <= n; ++i)
				s[i] = ' ';
			for (i = 1; i <= n; ++i)
			{
				tmp = group[belong[i].first];
				ans[tmp[(belong[i].second + k) % tmp.size()]] = s[i];
			}
			ans[n + 1] = 0;
			puts(ans + 1);
		}
		putchar(10);
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值