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 ...
随机推荐
- vue项目使用vue-photo-preview插件实现点击图片放大预览和移动
官方链接: http://npm.taobao.org/package/vue-photo-preview # 安装 npm install vue-photo-preview --save # 引入 ...
- PHP三种运行方式(转载)
三种运行方式:mod_php5.cgi.fast-cgi 1. 通过HTTPServer内置的模块来实现, 例如Apache的mod_php5,类似的Apache内置的mod_perl可以对perl支 ...
- [MySQL 5.6] information_schema.innodb_metrics
1. 概括 已关闭/打开的配置 use information_schema select count(*), status from innodb_metrics group by status; ...
- (1.8)mysql中的trace
(1.8)mysql中的trace 以json格式存储
- GUI库之Tkinter组件(二)
一.Lable组件 Lable组件是用于在界面上输出描述的标签: 1.举个例子. # Lable组件 from tkinter import * root = Tk() root.title(&quo ...
- maven 安装 配置
一.下载及安装 1.1 下载maven 3.1.1 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.1.1 ),下载完成后,解压到某个目录(本 ...
- 学习Shell(二)变量
如何给shell脚本传入参数 1.执行“vi test.sh”创建一个新的shell脚本. vi test.sh 2.脚本test.sh的内容如下: #!/bin/sh name=$ echo &qu ...
- Nginx Log日志统计分析常用命令
IP相关统计 统计IP访问量(独立ip访问数量) awk '{print $1}' access.log | sort -n | uniq | wc -l 查看某一时间段的IP访问量(4-5点) gr ...
- SVM高斯核为何会将特征映射到无穷维?【转载】
转自:https://www.zhihu.com/question/35602879 1.问题: SVM中,对于线性不可分的情况下,我们利用升维,把低维度映射到到维度让数据变得“更可能线性可分”,为了 ...
- Json常用操作
1, 获取json字符串中属性(传统方式) import net.sf.json.JSONObject JSONObject json = JSONObject.fromObject(response ...