Cipher

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19502   Accepted: 5239

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

第一次做这样的多个循环节。对每一个循环节中的元素分开求解的题目,记录一下。

题意:给出一个n个数的置换,依照置换规则把一个字符串置换k次,假设字符串的长度不足n,则在字符串末尾补空格。直到长度为n。求置换k次之后的字符串是什么。

分析:假设直接依照题目描写叙述的模拟,肯定会超时。

对整个字符串置换,能够转换为对每一个循环节进行置换。由于一个循环节里面的元素,对其它元素没有影响,这样我们就能够先求出全部的循环节和每一个循环节中的元素及循环节的长度。然后对每一个循环节中的元素进行置换。一个循环节中的元素置换k次,等价于每一个元素置换k%(这个元素所在循环节的长度)次,这样模拟次数变得非常小。就能够模拟了。

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std; const int N = 215;
char str[N]; //原始字符串
char ans[N]; //置换后的字符串
int key[N]; //置换规则
int cnt; //循环节数量
int num[N]; //每一个循环节的长度
int cir[N][N]; //cir[i][j]表示第i个循环节中的第j个元素相应的位置
int vis[N]; //记录每一个数是否訪问过 int n; void get_circle() {
memset(vis, 0, sizeof(vis));
memset(num, 0, sizeof(num));
cnt = 0;
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
vis[i] = 1;
num[cnt] = 0;
int tmp = key[i];
cir[cnt][num[cnt]++] = tmp;
while(!vis[tmp]) {
vis[tmp] = 1;
tmp = key[tmp];
cir[cnt][num[cnt]++] = tmp;
}
cnt++;
}
}
} int main() {
int k;
while(~scanf("%d",&n) && n) {
for(int i = 1; i <= n; i++)
scanf("%d", &key[i]);
get_circle();
while(~scanf("%d", &k) && k) {
gets(str);
int len = strlen(str);
for(int i = len; i <= n; i++)
str[i] = ' ';
for(int i = 0; i < cnt; i++) {
for(int j = 0; j < num[i]; j++) {
ans[cir[i][(j+k)%num[i]]] = str[cir[i][j]];
} //第i个循环节中的第j个元素置换k次之后的位置为第(j+k)% num[i]
}
ans[n+1] = '\0';
printf("%s\n", ans+1);
}
printf("\n");
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ 1026 Cipher(更换)的更多相关文章

  1. POJ 1026 Cipher(置换群)

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

  2. poj 1026 Cipher

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

  3. 【POJ】1026.Cipher

    题解 置换群的快速幂,然而我姿势水平不高,样例过不去,然后才明白这个置换的意思是这个位置上的数代表要把原位置的某个数换过来 需要新开一个数组存结果 代码 #include <iostream&g ...

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

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

  5. poj 1026(置换群)

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

  6. poj 2369 Permutations 更换水称号

    寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...

  7. poj 1026

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

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

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

  9. acm数学(待续)

    意图写出http://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html这个东西的完善版. 1.置换,置换的运算 poj 2369 Per ...

随机推荐

  1. Windows Phone开发(25):启动器与选择器之WebBrowserTask

    原文:Windows Phone开发(25):启动器与选择器之WebBrowserTask 从名字上就看出来,这个家伙就是打开浏览并浏览到指定页面. 它有两个用途完全一样的属性:Uri属性是Syste ...

  2. iOS执行时与method swizzling

    C语言是静态语言,它的工作方式是通过函数调用,这样在编译时我们就已经确定程序怎样执行的.而Objective-C是动态语言,它并不是通过调用类的方法来执行功能,而是给对象发送消息,对象在接收到消息之后 ...

  3. MultiROM for the XIAOMI MI2S/2C/2! (Kexec HardBoot Enabled with Kexec HardBoot Patch!)

    Introduction This is a port of Tassadar's MultiROM, a multi-boot mod for XIAOMI MI2/2S/2C. The main ...

  4. java JNI开发

    Jni程序开发的一般操作步骤如下: l         编写java中的调用类 l         用javah生成c/c++原生函数的头文件 l         c/c++中调用需要的其他函数功能, ...

  5. Android源码及SDK国内镜像下载

    Android源码及SDK国内镜像下载Android源码下载: 今天发现,清华大学提供AOSP镜像,以前都是从Google的站点下载同步更新的,但是现在有了国内的镜像站点就好多了,下载Androidd ...

  6. Apache Rewrite 理解

    因为工作须要,查了一下Apache的文档,对当中反向引用和条件的运行做了理解和实验,以下是对Apache 2.2文档的摘录,并在上面做了实验的样例说明,希望能给一些须要深入理解的一些帮助. 其它部分就 ...

  7. 让Android系统支持ubifs文件系统

    原文地址:http://www.cnblogs.com/linucos/p/3279381.html 1. ubifs号称性能比yaffs2 好,同时压缩可读写,文件系统image体较小同时可写,相当 ...

  8. Qt5该插件机制(2)--QxxxFactory类和QFactoryLoader类别

    <<<<<<<<<<<<<<<<<<<<<<<<< ...

  9. EL字符串表达式和常用功能用途拦截

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ${wj ...

  10. net搭建热插拔式web框架(沙箱的构建)

    net搭建热插拔式web框架(沙箱的构建) 上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章 ...