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 题意 : 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的更多相关文章

  1. JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher

    加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...

  2. POJ1026 Cipher(置换的幂运算)

    链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  3. BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6014  Solved: 2503[Submit ...

  4. freeCodeCamp:Caesars Cipher

    让上帝的归上帝,凯撒的归凯撒. 下面我们来介绍风靡全球的凯撒密码Caesar cipher,又叫移位密码. 移位密码也就是密码中的字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母 ...

  5. ural Cipher Message

    Cipher Message Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  6. 紫书例题-Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

  7. 【BZOJ-1031】字符加密Cipher 后缀数组

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5504  Solved: 2277[Submit ...

  8. uva-1339Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

  9. scp使用加密算法报错unknown cipher type

    为了提高scp的传输速度指定了scp的加密算法为arcfour $ scp -c arcfour localFile userName@remoteIP:remoteFile 得到报错unknown ...

  10. POJ - 1107 W's Cipher

    POJ - 1107 W's Cipher Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u De ...

随机推荐

  1. 用2个DATETIMEPICKER分别输入时间和日期,再合并成一个DATETIME类型

    DtpDate为日期的,DtpTime为时间的 StrToDateTime(FormatDateTime('yyyy-MM-dd', DtpDate.Date) + ' ' + TimeToStr(D ...

  2. myeclipse项目导入到eclipse, HttpServletRequest报红现象

    eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法 当使用eclipse导入外部的web工程时,有时会提示HttpServletRequest, Serv ...

  3. es倒排索引和正排索引

    搜索的时候,要依靠倒排索引:排序的时候,需要依靠正排索引,看到每个document的每个field,然后进行排序,所谓的正排索引,其实就是doc values.在建立索引的时候,一方面会建立倒排索引, ...

  4. 【Mock】【接口测试】【面试】mock-server 环境搭建—加分项!

    mock-server 环境搭建 前言 mock 除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的需要服务端提供 API 接口 此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发 ...

  5. 【剑指offer】两个链表的第一个公共结点

    一.题目: 输入两个链表,找出它们的第一个公共结点. 二.思路: 思路一:模拟数组,进行两次遍历,时间复杂度O(n2) 思路二:假定 List1长度: a+n  List2 长度:b+n, 且 a&l ...

  6. HP1020打印机“传递给系统调用的数据区域太小” 如何处理?

    如果电脑上曾经安装过 HP LaserJet 激光打印机的驱动程序,重新安装驱动程序之前,需要完全卸载以前安装的驱动程序,否则可能会出现无法找到设备或者安装不上驱动程序的现象. 安装网站下载的即插即用 ...

  7. crm 理解

    1:crm 就是一个 能够随时(通过labtop,mobile phone)掌握客户所有信息(过往的交易,)的工具.  通过足够的数据分析,能够更好的了解客户. 目的: a:使客户保留下来. b:赚取 ...

  8. Hybrid设计--核心交互

    普通网页中跳转使用a标签,这里我们要对跳转进行更多的干预,所以将全站的跳转收口到框架层,用forward去实现.拒绝用a和window.location.如果我想对所有跳转做一个处理,开动画或者对跳转 ...

  9. python demjson

    这个是第三方的json库 首先安装 http://deron.meranda.us/python/demjson/ demjson-2.2.4.tar.gz #tar -xzf demjson-2.2 ...

  10. 基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0

    基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0 目录 1. 开发环境2. 主要功能逻辑介绍3. 框架功能简介 4. 数据库的创建 5. 框架模块详细介绍6. Tes ...