306 - Cipher
Time limit: 3.000 secondshttp://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, , 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 , where 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 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 position , where 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.045s*/ /*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; }