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. 第三天--html区块

    <!Doctype html><html>    <head>        <meta charset="utf-8">      ...

  2. EF jsonignore

    页面单独指定不循环引用 [JsonIgnore] Newtonsoft.Json.JsonSerializerSettings jsSettings = new Newtonsoft.Json.Jso ...

  3. linux的相关指令命令

    ls:查看当前所在的目录 whoami:查看当前所在的用户名 who:(查看所有的正在使用的用户名) id:唯一的识别编号(组所在的识别编号) uname  -a:显示当前操作系统的版本 cd:切换工 ...

  4. webstorm添加*.vue文件代码提醒支持webstorm支持es6vue里支持es6写法

    本文转自:http://www.lred.me/2016/01/07/webstorm%E6%B7%BB%E5%8A%A0-vue%E6%96%87%E4%BB%B6%E6%94%AF%E6%8C%8 ...

  5. jquery 替换img 属性

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. 【笔记】mysql两条数据的某个属性值互换

    update groupuser as g1 join groupuser as g2 on (g1.user_id=1 and g2.user_id = 2) or(g1.user_id = 2 a ...

  7. struts 2 时间控件

    在使用struts2框架时,为我们提供了时间选择器控件:datetimepicker.但是在使用过程中会出现一些问题,主要就是struts2版本更新时做了一些修改.在struts2.0时,使用< ...

  8. 比较完整的WebView的用法

    WebView, WebChromeClient和WebViewClient加载网页基本用法 webview是android中的浏览器控件,在一些手机应用中常会用到b/s模式去开发应用,这时webvi ...

  9. eclipse 创建maven web项目

    参考:http://www.cnblogs.com/hongwz/p/5456616.html eclipse 创建maven web项目

  10. [原创]Centos7 从零整合LNMP一体包

    按照前几章配置好后,我们就可以把这些工具打包啦.生成LNMP一体包. # export LD_LIBRARY_PATH=/package/libmemcached/lib:$LD_LIBRARY_PA ...