POJ 1026 Cipher(更换)
Cipher
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 19502 | Accepted: 5239 |
Description
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
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
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(更换)的更多相关文章
- POJ 1026 Cipher(置换群)
题目链接 题意 :由n个数字组成的密钥,每个数字都不相同,都在1-n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求 ...
- poj 1026 Cipher
置换群就可以搞定!!! 注意下格式就好了…… #include<iostream> #include<stdio.h> #include<algorithm> #i ...
- 【POJ】1026.Cipher
题解 置换群的快速幂,然而我姿势水平不高,样例过不去,然后才明白这个置换的意思是这个位置上的数代表要把原位置的某个数换过来 需要新开一个数组存结果 代码 #include <iostream&g ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- poj 1026(置换群)
题意:给你一个变换规则,和一个字符串,问经过k次变换后得到的字符串. 思路:开始的时候试图去找它的整个周期,谁知道周期太大了,各种RE,后来在得知此题需要用置换群来优化,第一次接触置换群学习了下! 代 ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- poj 1026
这题一开始没看清楚 等级差距不超过1 1->2->3 就是错误的,因为3-1==2 ,意思是间接的也不行 其次等级最小是1,最大是n 你要到达1号首领的位置 假设1号等级x,限制m,最大上 ...
- POJ 1026 置换群的k次幂问题
题目大意: 给定了一组对应关系,经过k次幂后,得到新的对应关系b[i],然后将给定的字符串上的第i位字符放置到b[i]的位置上, 如果字符串长度不足n就用空格补足,这里的是空格,也就是str[i] = ...
- acm数学(待续)
意图写出http://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html这个东西的完善版. 1.置换,置换的运算 poj 2369 Per ...
随机推荐
- 【甘道夫】Hive 0.13.1 on Hadoop2.2.0 + Oracle10g部署详细解释
环境: hadoop2.2.0 hive0.13.1 Ubuntu 14.04 LTS java version "1.7.0_60" Oracle10g ***欢迎转载.请注明来 ...
- UNIX环境高级编程——Linux进程地址空间和虚拟内存
一.虚拟内存 分段机制:即分成代码段,数据段,堆栈段.每个内存段都与一个特权级相关联,即0~3,0具有最高特权级(内核),3则是最低特权级(用户),每当程序试图访问(权限又分为可读.可写和可执行)一个 ...
- mysql多实例的配置(转)
1.创建多实例的目录: mkdir -p /data/mysql/mysql_3307/{data,tmp,logs} mkdir -p /data/mysql/mysql_3308/{data,tm ...
- 使用Spring的@Autowired 实现DAO, Service, Controller三层的注入(转)
简述: 结合Spring和Hibernate进行开发 使用@Autowired实现依赖注入, 实现一个学生注册的功能,做一个技术原型 从DAO(Repository) -> Service -& ...
- lunix shell 基础经常使用整理
1 ps -ef 显示正在执行的进程,pid 等信息 UID PID PPID C STIME TTY TIME CMD root 1 0 0 03:45 ? 00:00:02 ini ...
- Redis整合Spring结合使用缓存实例(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
- HIPO图
HIPO图(Hierarchy Plus Input/Processing/Output)是表示软件结构的一种图形工具.以模块分解的层次性以及模块内部输入.处理.输出三大基本部分为基础建立的.它由两部 ...
- ZOJ Monthly, October 2010 ABEFI
ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...
- 站点系统压力測试Jmeter+Badboy
近期项目须要压力測试,因此搜了几款试用,首选的是LoadRunner这款大名鼎鼎的測试软件: LoadRunner11 下载请猛戳这里 传送门LoadRunner破解文件 下载请猛戳这里 传送门Loa ...
- Hadoop之—— CentOS Warning: $HADOOP_HOME is deprecated解
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46389499 启动Hadoop时报了一个警告信息.我安装的Hadoop版本号是ha ...