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 ...
随机推荐
- 初识google多语言通信框架gRPC系列(三)C#中使用gRPC
我的这几篇文章都是使用gRPC的example,不是直接编译example,而是新建一个项目,从添加依赖,编译example代码,执行example.这样做可以为我们创建自己的项目提供借鉴.如果对gR ...
- 分布式文件系统FastDFS介绍和配置过程
http://ylw6006.blog.51cto.com/470441/948729/ 由于网站使用nfs共享方式保存用户上传的图片,附件等资料,然后通过apache下载的方式供用户访问,在网站架构 ...
- VS2012的变态优化,双循环变单循环
VS2010也是一样
- 输入框 js正则推断输入
1.文本框仅仅能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onaf ...
- NLB+Application Request Route 网路负载均衡
NLB网路负载均衡管理器详解 序言 在上一篇配置iis负载均衡中我们使用啦微软的ARR,我在那篇文章也中提到了网站的高可用性,但是ARR只能做请求入口的消息分发服务,这样如果我们的消息分发服务器给 ...
- C++笔试面试总结
手游广州某公司书面今天接受采访时.刚进去中午1中场休息.他们公司谁刚刚醒来,一个冷漠打牌,然后去上班.瞬间,这些公司有没有什么好印象,压抑. 接着快2点的时候.发了一份笔试题.大部分题目均在网上的&l ...
- 2048 Puzzle游戏攻略
2048 Puzzle这是目前手机游戏的很火. 在地铁上经常看到的人玩这个游戏. 首先,简介2048 Puzzle游戏. 游戏界面是4X4广场格,每一方格可以放置在数字. 有四种移动数字的方法,向左. ...
- 经纬Zhang英拉垫背的企业家VC没有到这种地步这么卑鄙
经纬的合伙人张颖前不久发了一篇名为<泡沫就在那里------致经纬系CEO的一封信>,这篇文章所提到的泡沫论在业界引发了强烈的反响,随后有评论觉得,这明明是VC(风险投资)们自己的危机,为 ...
- 【DP|多重背包可行性】POJ-1014 Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of mar ...
- 《炉石传说》建筑设计欣赏(6):卡&在执行数据时,组织能力
上一篇文章我们看到了<炉石传说>核心存储卡的数据,今天,我们不断探索卡&身手. 基本的类 通过之前的分析,卡牌&技能涉及到几个类体系:Entity.Actor.Card.S ...