poj1026

Cipher










Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18970   Accepted: 5067

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

求置换的循环节

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #define MAX 202
5 int key[MAX], t[MAX];
6 void get_time(int n)
7 {
8 int i, j, count;
9 for (i = 1; i <= n; i++)
10 {
11 j = key[i];
12 count = 1;
13 while(i != j)
14 {
15 count++;
16 j = key[j];
17 }
18 t[i] = count;
19 }
20 }
21 int main()
22 {
23 int i, j, k, n, len;
24 char src[MAX], dst[MAX];
25
26 while(scanf("%d", &n)&&n)
27 {
28 for (i = 1; i <= n; i++)
29 scanf("%d", &key[i]);
30 memset(t, 0, sizeof(t));
31 get_time(n);
32 while(scanf("%d", &k)&&k)
33 {
34 getchar();
35 gets(src + 1);
36 for (i = strlen(src + 1) + 1; i <= n; i++)
37 src[i] = ‘ ‘;
38 src[n + 1] = 0;
39 for (i = 1; i <= n; i++)
40 {
41 int pos = i;
42 for (j = 0; j < k % t[i]; j++)
43 pos = key[pos];
44 dst[pos] = src[i];
45 }
46 dst[n + 1] = 0;
47 puts(dst + 1);
48 }
49 printf("\n");
50 }
51 return 0;
52 }

时间: 2024-08-04 13:59:36

poj1026的相关文章

POJ1026 Cipher 【polya】

This question is not so difficult. First,my thoughts were we should use a lot of code to find out the loop block,but there is no need to do that . you just need to get every new position of char in the string. Algorithm is also easy , just like how d

poj1026 Cipher

题目意思可概括为给定集合S = {1,..,n}的一个双射关系f, 求经过k次复合之后元素i对应的元素fk(i) (i∈S). 由于函数是双射,一个原像对应唯一一个像,同样一个像有唯一一个原像,考虑整个映射关系,存在整数m∈ Z,使得fm=f0=I. 即具有周期性. 每个元素映射回它自己有独立的周期T(i),整个映射的周期T=lcm(T(i)), i ∈ S. 独立处理更快,但对于本题也是刚刚卡过. 当然如果事先把所有询问读入加以预处理或者直接全部预处理会更快. 样例代码860ms卡过. htt

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po

POJ题目推荐(转载)

POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉.2.标记为A and B的题目是比较相似的题目,建议大家两个一起做,可以对比总结,且二者算作一个题目.3.列表中大约有70个题目.大家选做其中的50道,且每类题目有最低数量限制.4.这里不少题目在BUPT ACM FTP上面都有代码,请大家合理利用资源.5.50个题目要求每个题目都要写总结,养成良好的习惯.6.这个列表的目的在于让大家对各个方面的算法有个了解,也许要求有些苛刻,教条,请大家谅

POJ题目分类

初期: 一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,he

嗷嗷嗷,kuangbin大大博客上拉的题

正在学(learning),未学(waiting),已学(cut  vovering) 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.  

转:转一个搞ACM需要的掌握的算法. .

要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来.  适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什么就眼红,  发挥自己的长处,这才是重要的. 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,  因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打  出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)

算法初学者指南

摘自网络,对于这个训练计划,我只能膜拜,~ 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15 分钟内打完,甚至关掉显示器都可以把程序打 出来. 1.最短路(Floyd.Dijstra,BellmanFord) 2. 最小生成树(先写个prim,kruscal要用并查集,不好写) 3.大数(高精度)加减乘除 4.二分查找. (代码可在五行以内) 5.叉乘.判线段相交.然后写个凸包. 6.BFS.DFS,同时熟练hash表(