Cipher(置换群)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20821 | Accepted: 5708 |
Description
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
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
题解:
给你一个位置,再给你字符串,问转换k次后的字串,暴力了下,超时了。。。然后就是用置换群,找出循环节,主要是ans[b[(j+k)%num]]=s[b[j]];
每一个位置变换(k%num)次
大神的解释拿来:
对于置换的数组(我们用next数组代替),里面的每一个元素都会存在于(有且只有)一个置换子群里面。
例如 题中数据: (4 -> 7 -> 1 -> 4)(5 -> 2 -> 3 -> 3) (8 -> 6 -> 8)(10 -> 9 -> 10)。 可找到4个子群。
思路:每个子群都有一定数目的各不相同的元素,用变量t记录子群元素个数,用son[]存储子群元素,就有son[0],son[1]...son[t-1]个元素。
那么我们可以得到如下变换规则-> 新字符数组gain[son[(k + j) % t]] = 原字符数组str[son[j]] (0 <= j <= t-1)。
公式推导过程:一个子群 有t个元素说明它的循环节为t, 经过k次置换 那么肯定就有son[(k + j) % t] = son[j];
例如 题目数据:4-1 5-1 3-1 7-1 2-1 8-1 1-1 6-1 10-1 9-1 (为了对应字符串的下标自减1) Hello+Bob+ 空格用+表示
拿子群(3 -> 6 -> 0 -> 3【已经减一】)来说,其中t = 3,son[0] = 3, son[1] = 6, son[2] = 0。
当k = 1时, { j = 0,son[(k + j) % t] = 6,gain[6] = str[3] = l
j = 1,son[(k + j) % t] = 0,gain[0] = str[6] = B
j = 2,son[(k + j) % t] = 3,gain[3] = str[0] = H
}
暴力超时:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=;
int pos[MAXN];
int main(){
int N;
while(~scanf("%d",&N),N){
int k;
char s[MAXN],ans[MAXN];
for(int i=;i<N;i++)SI(pos[i]);
while(scanf("%d%*c",&k),k){
mem(s,);
gets(s);
while(k--){
for(int i=;i<N;i++){
if(i>=strlen(s))ans[pos[i]-]=' ';
else ans[pos[i]-]=s[i];
}
ans[N]='\0';
strcpy(s,ans);
}
puts(ans);
}
}
return ;
}
置换群:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=;
int next[MAXN];
int vis[MAXN];
char ans[MAXN];
int b[MAXN];
int main(){
int N;
while(~scanf("%d",&N),N){
int k;
char s[MAXN];
for(int i=;i<N;i++)SI(next[i]),next[i]--;
while(scanf("%d%*c",&k),k){
gets(s);
mem(vis,);
for(int i=strlen(s);i<N;i++)s[i]=' ';
for(int i=;i<N;i++){
if(!vis[i]){
int j=i,num=;
while(!vis[j]){
vis[j]=;
b[num++]=j;
j=next[j];
}
for(j=;j<num;j++){
ans[b[(j+k)%num]]=s[b[j]];
}
}
}
ans[N]='\0';
puts(ans);
}
puts("");
}
return ;
}
Cipher(置换群)的更多相关文章
- poj1026 Cipher ——置换群
link:http://poj.org/problem?id=1026 其实这道题目和poj2369这道题目一样. 都是基础的置换群题目.把那道题目理解了,这道题就没问题了. 不过我的方法貌似比较挫, ...
- POJ 1026 Cipher(置换群)
题目链接 题意 :由n个数字组成的密钥,每个数字都不相同,都在1-n之间,有一份长度小于等于n的信息,要求将信息放到密钥下边,一一对应,信息不足n的时候补空格,然后将位置重新排列,将此过程重复k次,求 ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- POJ1026 Cipher(置换的幂运算)
链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- freeCodeCamp:Caesars Cipher
让上帝的归上帝,凯撒的归凯撒. 下面我们来介绍风靡全球的凯撒密码Caesar cipher,又叫移位密码. 移位密码也就是密码中的字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母 ...
- ural Cipher Message
Cipher Message Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- 紫书例题-Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
- 【BZOJ-1031】字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5504 Solved: 2277[Submit ...
随机推荐
- FPGA的LE数与门数的关系(转)
一般而言FPGA等效门数的计算方法有两种 一是把FPGA基本单元(如LUT+FF,ESB/BRAM)和实现相同功能的标准门阵列比较,门阵列中包含的门数即为该FPGA基本单元的等效门数,然后乘以基本单元 ...
- Filter与Servlet的区别和联系
Filter Servlet 接口 实现Filter接口 实现Servlet接口 使用步骤 1.创建类,继承接口 2.实现方法 init() doFilter() destroy() 3.配置WEB- ...
- javascript的层次
1.功能api 2.代码organization 3.performance 4.work flow
- 一道C语言面试题:判断字串是否可以通过重新排列字母使之对称
题目:输入一个字符串,如“adcaeceeed”,判断是否可以通过重新排列使之可以输出对称字符串,如本例可以输出“adceeeecda”,返回True. 来源:某500强企业面试题目 思路:扫描字串, ...
- linux脚本之简单实例
利用脚本计算10的阶乘 简单说明一下: #!/bin/bash说明该shell使用的bash shell程序.这一句不可少for i in `seq 1 10`还可以写成for i in 1 2 3 ...
- win7 Visual Studio 2008 安装程序时出现“ 加载安装组件时遇到问题。取消安装。”处理方法
win7 Visual Studio 2008 安装程序时出现“ 加载安装组件时遇到问题.取消安装.”处理方法 vs2008试用期到期,卸载.重新安装都会出现“ 加载安装组件时遇到问题.取消安装.”无 ...
- Makefile里调用Shell注意点
http://www.linuxidc.com/Linux/2012-04/59093.htm 大家经常编写和使用Makefile, Makefile里面也经常用到shell, 但对其中一些需要注意的 ...
- Hadoop 4、Hadoop MapReduce的工作原理
一.MapReduce的概念 MapReduce是hadoop的核心组件之一,hadoop要分布式包括两部分,一是分布式文件系统hdfs,一部是分布式计算框就是mapreduce,两者缺一不可,也就是 ...
- N1必备外来语
201412: キャリア: ノルマ:(normal)定额,劳动基本定额. チーフ: コスト: バンド:[英] band;皮带,腰带:吹奏乐团,轻音乐乐团(演奏者の一団.楽団.楽隊):营居群 [2014 ...
- python安装完毕后,提示找不到ssl模块的解决方示
python安装完毕后,提示找不到ssl模块: [root@localhost ~]# python2.7.5 Python 2.7.5 (default, Jun 3 2013, 11:08:43) ...