Cipher
Description
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
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
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 题意 : Bob和Alice想用一种新的加密方式,他们编码和译码都是依靠秘密钥匙,秘密钥匙选用一串数字a1,……an(0<ai<=n),编码要求:将信息(长度不大于n,如果小于n请自己补上空格)中i位置上的字符放到ai位置上
。这个过程经过K遍之后就成了加密后的信息。现在要求你写一个程序帮他们实现编码。 思路: 因为K会很大, 暴力会超时, 将其看作一个置换群, 求出每个循环的长度, 可知一个循环的长度即他的周期; 考虑求出每个循环的长度L, 对K取模, 然后再暴力求解;
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn= ; int main()
{
int n;
while(scanf("%d", &n) && n)
{
int hay[maxn], pos[maxn];
for(int i = ;i < n; i++)
{
scanf("%d",&hay[i]);
hay[i] -= ;
}
for(int i =;i < n;i++)
{
pos[i] = ;
int tmp = hay[i];
while(tmp!=i)
{
pos[i]++;
tmp = hay[tmp];
}
}
int k;
char s1[maxn], s2[maxn];
while(scanf("%d", &k) && k)
{
getchar();
gets(s1);
if(strlen(s1) < n)
{
for(int i = strlen(s1); i<n; i++)
s1[i] = ' ';
s1[n] = '\0';
}
for(int i = ; i < n; i++)
{
int tim = k%pos[i];
int t = i;
while(tim--)
{
t = hay[t];
}
s2[t] = s1[i];
}
s2[n] = '\0';
puts(s2);
}
printf("\n");
}
return ; }
Cipher的更多相关文章
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- POJ1026 Cipher(置换的幂运算)
链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- freeCodeCamp:Caesars Cipher
让上帝的归上帝,凯撒的归凯撒. 下面我们来介绍风靡全球的凯撒密码Caesar cipher,又叫移位密码. 移位密码也就是密码中的字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母 ...
- ural Cipher Message
Cipher Message Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- 紫书例题-Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
- 【BZOJ-1031】字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5504 Solved: 2277[Submit ...
- uva-1339Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
- scp使用加密算法报错unknown cipher type
为了提高scp的传输速度指定了scp的加密算法为arcfour $ scp -c arcfour localFile userName@remoteIP:remoteFile 得到报错unknown ...
- POJ - 1107 W's Cipher
POJ - 1107 W's Cipher Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u De ...
随机推荐
- SQLAlchemy中时间格式化及将时间戳转成对应时间的方法-mysql
https://blog.csdn.net/guoqianqian5812/article/details/80175866 方法很简答,都是借助于mysql数据库提供的函数将时间格式化方法 func ...
- linux 系统中 /etc/passwd 和 /etc/shadow文件详解
链接地址:http://blog.csdn.net/yaofeino1/article/details/54616440
- Mysql 通过information_schema爆库,爆表,爆字段
MySQL版本大于5.0时,有个默认数据库information_schema,里面存放着所有数据库的信息(比如表名. 列名.对应权限等),通过这个数据库,我们就可以跨库查询,爆表爆列. 若要从这些视 ...
- [GDOI2018]滑稽子图
题目链接:[被和谐] 题目大意:对于一棵树$(V,E)$,对于$S\subset V$,$f(S)$为点集$S$的导出子图的边数.求$\sum_{S\subset V}f(S)^k$ 这里的导出子图说 ...
- 【SQL】where 后不可以接聚合函数,都哪些是聚合函数?
where 后不可以接聚合函数,比如函数:SUM(count),AVG(count),MIN(count),MAX(count)
- 轻量级ORM工具Simple.Data
今天推举的这篇文章,本意不是要推举文章的内容,而是据此介绍一下Simple.Data这个很有意思的类ORM工具. 现在大家在.NET开发中如果需要进行数据访问,那么基本都会使用一些ORM工具,比如微软 ...
- Spark之数据倾斜 --采样分而治之解决方案
1 采样算法解决数据倾斜的思想 2 采样算法在spark数据倾斜中的具体操作
- 浅谈Python web框架
一.Python web框架 Web Framework,Ruby的世界Rails一统江湖,而Python则是一个百花齐放的世界,各种micro-framework.framework不可胜数,不完全 ...
- 【LeetCode每天一题】Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- python中的filter、map、reduce、apply用法
1. filter 功能: filter的功能是过滤掉序列中不符合函数条件的元素,当序列中要删减的元素可以用某些函数描述时,就应该想起filter函数. 调用: filter(function,seq ...