Cipher

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
    给出一个字符串和一个置换,询问经过k次这个置换之后得到的新的字符串,还是找出循环节之后模拟即可。
    
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define PI acos(-1.0)
char s[];
char ans[];
bool v[];
int a[];
int main()
{
int n,i,j,k,d;
while(cin>>n&&n){
for(i=;i<=n;++i){
scanf("%d",&d);
a[d]=i;
}
while(cin>>k&&k){
memset(s,,sizeof(s));
getchar();
gets(s+);
for(i=;i<=n;++i)if(s[i]=='\0')s[i]=' ';
vector<int>vi;
memset(v,,sizeof(v));
memset(ans,,sizeof(ans));
for(i=;i<=n;++i){
if(!v[i]){
vi.clear();
j=i;
while(!v[j]){
vi.push_back(j);
v[j]=;
j=a[j];
}
for(j=;j<vi.size();++j){
ans[vi[j]]=s[vi[(j+k%vi.size())%vi.size()]];
}
}
}
cout<<ans+<<endl;
}
cout<<endl;
}
return ;
}
												

poj-1026-置换的更多相关文章

  1. poj3270 && poj 1026(置换问题)

    | 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...

  2. poj 3270 置换

    poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...

  3. POJ 1026 Cipher(更换)

                                                                   Cipher Time Limit: 1000MS   Memory Li ...

  4. POJ 1026 Cipher(置换群)

    题目链接 题意 :由n个数字组成的密钥,每个数字都不相同,都在1-n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求 ...

  5. poj 1026 Cipher

    置换群就可以搞定!!! 注意下格式就好了…… #include<iostream> #include<stdio.h> #include<algorithm> #i ...

  6. poj 1026(置换群)

    题意:给你一个变换规则,和一个字符串,问经过k次变换后得到的字符串. 思路:开始的时候试图去找它的整个周期,谁知道周期太大了,各种RE,后来在得知此题需要用置换群来优化,第一次接触置换群学习了下! 代 ...

  7. poj 1026

    这题一开始没看清楚 等级差距不超过1 1->2->3 就是错误的,因为3-1==2 ,意思是间接的也不行 其次等级最小是1,最大是n 你要到达1号首领的位置 假设1号等级x,限制m,最大上 ...

  8. poj 3270(置换 循环)

    经典的题目,主要还是考思维,之前在想的时候只想到了在一个循环中,每次都用最小的来交换,结果忽略了一种情况,还可以选所有数中最小的来交换一个循环. Cow Sorting Time Limit: 200 ...

  9. POJ 1026 置换群的k次幂问题

    题目大意: 给定了一组对应关系,经过k次幂后,得到新的对应关系b[i],然后将给定的字符串上的第i位字符放置到b[i]的位置上, 如果字符串长度不足n就用空格补足,这里的是空格,也就是str[i] = ...

  10. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

随机推荐

  1. js作用域题目

    window.number = 4var obj = { 'number': 4, 'tbl': (function(){ this.number *= 2; console.log(this.num ...

  2. Codeforces 85 D. Sum of Medians

    题目链接:http://codeforces.com/contest/85/problem/D 做法果然男默女泪啊..... 大概就是直接开了一个$vector$每次插入删除都用自带的$insert$ ...

  3. 【BZOJ】3209: 花神的数论题

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3209 显然是按照二进制位进行DP. 考虑预处理$F[i][j]$表示到了二进制的第$i$位 ...

  4. hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  5. js code

    //在页面增加一个放置图标的区块 if(!document.getElementById('_span_jiucuo')) document.write("<span id='_spa ...

  6. 学习笔记21—PS换图片背景

    将照片红底的换成白底的. 操作步骤: 1 先上效果,照片来自网络反正不认识,法律问题找度娘 2 下面开始操作,打开图片进入通道面板,选择照片底色的那个通道,复制并调整色阶,确保黑白分明 3 回到图层面 ...

  7. vue 定义全局变量在一个组件内引用

    第一步: 第二步: 第三步: ok!!完了,当然了,你也可以在 main.js里面全局引用,然后用原型链挂在vue上面,用this的方法去获取!!

  8. ggplot2画图

    早在N年前就听说这个包画图不错,一直没机会用,终于等到了.相比前面trendline这个包的可视化功能强大得多. ggplot2需要使用dataframe,其实就是一个N维数组, install.pa ...

  9. Python 编程快速上手 第十一章 Web scrapping

    前言 这一章讲了如何在 Web 上抓取相关的信息,工具是三个模块: webbrowser 模块:用于打开浏览器指定页面 requests 模块:用于下载文件 Beautiful Soup 模块:用于解 ...

  10. G.711是一种由国际电信联盟(ITU-T)制定的音频编码方式

    http://zh.wikipedia.org/zh-cn/G.711 ITU-T G.711 page ITU-T G.191 software tools for speech and audio ...