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. day4_修改文件

    修改文件有两种方式:一种是把文件的全部内容都读到内存中,然后把原有的文件内容清空,重新写新的内容:第二种是把修改后的文件内容写到一个新的文件中 第一种:一次性把文件全部读到,读到内存这个能,这种文件小 ...

  2. 图->存储结构->邻接多重表

    文字描述 邻接多重表是无向图的另一种链式存储结构. 虽然邻接表是无向图的一种很有效的存储结构,在邻接表中容易求得顶点和边的各种信息. 但是,在邻接表中每一条边(vi,vj)有两个结点,分别在第i个和第 ...

  3. 内部排序->其它->地址排序(地址重排算法)

    文字描述 当每个记录所占空间较多,即每个记录存放的除关键字外的附加信息太大时,移动记录的时间耗费太大.此时,就可以像表插入排序.链式基数排序,以修改指针代替移动记录.但是有的排序方法,如快速排序和堆排 ...

  4. Excel--数据分列功能

    原文:http://www.ittribalwo.com/article/3963.html excel分列功能一:按照固定宽度进行数据拆分 情景: 如下图所示,在日常工作中,我们经常需要根据人员的身 ...

  5. Delphi 打开网址

    1. 通过iexplore.exe打开:ShellExecute(0, 'open', 'iexplore.exe', PChar('http://www.100xuekao.com'), '', S ...

  6. 《mongoDB》查询

    一:简单查询 db.collection.find(query, projection) - query :可选,使用查询操作符指定查询条件 - projection :可选,使用投影操作符指定返回的 ...

  7. 利用Python进行自然语言处理(笔记)第一章

    一.标识符 一个我们想要放在一组对待的字符序列--如:”hairy“.”his“或者”:“: 二.词类型 一个词在一个文本中独一无二的出现形式或拼写,也就是说这个词在词汇表中是唯一的.如果我们计数的项 ...

  8. python编码类型互转总结

    1.只有在unicode下才能将utf-8与gbk互转2.unicode是在内存中使用,bytes是文件存储和网络传输时使用-------------------------------------- ...

  9. You-Get——基于Python3的媒体下载工具

    You-Get是一个基于 Python 3 的下载工具.使用 You-Get 可以很轻松的下载到网络上的视频.图片及音乐. 项目主页:https://github.com/soimort/you-ge ...

  10. JavaScript学习笔记--语法

    代码风格: 每句结尾不用加分号: 单行注释用//,多行注释用/*...需要注释掉的代码....*/ 严格区分大小写 优雅的代码需要注意锁进 基础知识: Number:JavaScript不区分整数和浮 ...