Cipher
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21436   Accepted: 5891

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 置换的幂运算也可以用快速幂来做 代码:
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 205
int a[MAXN];
int n;
void zh(int* a,int *b)
{
int temp[MAXN];
int temp1[MAXN];
int i;
for(i=;i<=n;i++)
{
temp[i]=a[i];
temp1[i]=b[i];
}
for(i=;i<=n;i++)
a[i]=temp1[temp[i]];
}
void solve(int k,int *re)
{
int i;
int ans[MAXN];
int temp[MAXN];
for(i=;i<=n;i++)
{
temp[i]=a[i];
ans[i]=i;
}
while(k)
{
if(k&)
zh(ans,temp);
zh(temp,temp);
k>>=;
}
for(i=;i<=n;i++)
{
re[ans[i]]=i;
}
}
int main()
{
int k;
int i;
char str[MAXN];
int re[MAXN];
int len;
int cnt;
char ch;
while(scanf("%d",&n))
{
if(n==)
break;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
while(scanf("%d",&k))
{
if(k==)
break;
getchar();
memset(str,' ',sizeof(str));
len=;
while((ch=getchar())!='\n')
str[len++]=ch;
solve(k,re);
cnt=;
for(i=;i<=n;i++)
{
printf("%c",str[re[i]-]);
if(re[i]-<len)
cnt++;
}
printf("\n");
}
printf("\n");
}
return ;
}

POJ1026 Cipher(置换的幂运算)的更多相关文章

  1. 组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)

    CARDS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1448   Accepted: 773 Description ...

  2. poj 3128 Leonardo&#39;s Notebook(置换的幂)

    http://poj.org/problem?id=3128 大致题意:输入一串含26个大写字母的字符串,能够把它看做一个置换.推断这个置换是否是某个置换的平方. 思路:具体解释可參考url=ihxG ...

  3. poj 3128 Leonardo's Notebook (置换群的整幂运算)

    题意:给你一个置换P,问是否存在一个置换M,使M^2=P 思路:资料参考 <置换群快速幂运算研究与探讨> https://wenku.baidu.com/view/0bff6b1c6bd9 ...

  4. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  5. 算数运算符: + - * / //(地板除) %(取余) **(幂运算) / 比较运算符 > < >= <= == !=

    # ### python运算符 #(1) 算数运算符: + - * / //(地板除) %(取余) **(幂运算) var1 = 5 var2 = 8 # +res = var1 + var2 pri ...

  6. 求幂运算、多项式乘法及Horner法则的应用

    一,两种不同的求幂运算 求解x^n(x 的 n 次方) ①使用递归,代码如下: private static long pow(int x, int n){ if(n == 0) return 1; ...

  7. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  8. Python3基础 ** 幂运算 // 整除运算

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. 《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995

    POJ3641 此题应归类为素数. POJ1995 http://poj.org/problem?id=1995 题意 求(A1^B1+A2^B2+ - +AH^BH)mod M. 思路 标准快速幂运 ...

随机推荐

  1. Express的搭建--nodejs的学习

    1,需要的学习环境 Node.js: 0.10.32 Express: 4.10.2 MongoDB: 2.6.1 2,安装Express $ npm install -g express-gener ...

  2. 自定义注解之运行时注解(RetentionPolicy.RUNTIME)

    对注解概念不了解的可以先看这个:Java注解基础概念总结 前面有提到注解按生命周期来划分可分为3类: 1.RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成clas ...

  3. Linux 昨天时间

    今天date +%F昨天date -d yesterday +%F明天date -d tomorrow +%F七天前date -d "7 days ago" +%F

  4. 【转】Caffe初试(七)其它常用层及参数

    本文讲解一些其它的常用层,包括:softmax-loss层,Inner Product层,accuracy层,reshape层和dropout层及它们的参数配置. 1.softmax-loss sof ...

  5. javascript面向切面

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. electrica writeup

    关于 caesum.com 网上上的题目,分类有Sokoban,Ciphers,Maths,Executables,Programming,Steganography,Misc.题目有点难度,在努力奋 ...

  7. scikit-learn算法选择图

    图片来自sklearn官网 最近事情弄完一部分了,继续开始python data science!

  8. 为什么说在使用多条件判断时switch case语句比if语句效率高?

    在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...

  9. C++预定义宏

    C/C++宏体中出现的#,#@,##: - #的功能是将其后面的宏参数进行字符串化操作(stringfication),就是对它所引用的宏变量通过替换后在其左右各加上一个双引号 -##被称为连接符(c ...

  10. 使用WCF 创建 Rest service

    REST SERVICE 允许客户端修改url路径,并且web端功过url 请求数据. 他使用http协议进行通讯,想必大家都知道 . 并且我们可以通过设置进行数据类型转换, 支持XML,JSON 格 ...