当前位置:网站首页>POJ 1026 Cipher (置换群)
POJ 1026 Cipher (置换群)
2022-08-10 10:32:00 【51CTO】
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
Sample Output
题意
给出一个 n
个数的置换,按照置换的规则将一个字符串置换 k
次,如果字符串长度不足 n
,则在末尾补空格,求置换 k
次之后的字符串是什么。
思路
本来打算模拟出一个置换的前 100000 种情况,然后把所有的结果存储在一个数组中。
只是这道题目只给了 10M
的内存限制,提交了好几次都是内存超限,再小一点又是运行错误了。
最后改成 【置换 + 轮换 + 模】 的方法之后就可以过了。
把给出的 key
写成置换的形式,然后对其分解求出每部分的轮换以及轮换的长度,因为在变换过程中每一个轮换中的元素所占有的位置不会改变,所以只是这些局部的数字会循环变化,循环的长度也就是轮换的长度。
AC 代码
边栏推荐
猜你喜欢
chart.js水平柱状图插件
这些年我开源的几个小项目
leetcode:334. 递增的三元子序列
大连理工&鹏城&UAE提出用于伪装目标检测的混合尺度三重网络ZoomNet,性能SOTA!
What is an abstract class
js guessing game source code
谷歌数据中心发生“电力事故”造成 3 人受伤
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
跨公网环境,路由策略,进行设备的访问
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
随机推荐
js对象转FormData对象(一般用于上传)
8月份DB-Engines 数据库排行榜最新战况
这些年我开源的几个小项目
WebView2 通过 PuppeteerSharp 实现爬取 王者 壁纸 (案例版)
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
网络文化经营许可证
Taro小程序跨端开发入门实战
C#List的使用以及Linq的使用
Gartner reiterates the important value of 'data weaving'
Text selection rounded style border-radius
ZZULIOJ 1116 Delete elements [delete]
[C language] Floating point number rounding
Regarding the missing json converter, the error message is: No converter found for return value of type
Dialogue with Chen Ciliang: Nezha wants to popularize high-end products
Redis (three) - detailed configuration file, publish and subscribe, new data types
使用cpolar远程连接群晖NAS(升级固定链接2)
Swin Transformer作者曹越加入智源,开展视觉基础模型研究
"Chief Engineer" Principal (Principal) engineer's way of training
14 high-frequency handwritten JS interview questions and answers to consolidate your JS foundation
owl.carousel poster card Slider carousel switch