[ZOJ 1009] Enigma (模拟)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1009
题目大意:给你三个转换轮,只有当第一个转换轮转动一圈后第二个才会转,当第二个转动一圈后第三个才会转。转换轮的意思是我按动一个按钮,显示器经过转换轮的转换显示另外一个字母。每按下一个按钮,第一个转换轮都会转动一次。
叉姐说得好,多学习一下思维方法,有些问题都是能够很高效率的想出来的。脑洞什么的全是骗人的。
注意看这张图:

中间转动轮的点, 左右两边是一一对应的。
也就是说,无论转动轮怎么转,都能把左边的keyboard和右边的display一一对应上,不重不漏。
那么,我们给rotor左边的接口依次标号1,2,3,4,5,6 并且根据第一个状态读出对应关系。即1,-1,1,2,0,-3
经过对应关系,我们把keyboard上的标为A集合{a,b,c,d,e,f} 经过对应关系B {1,-1,1,2,0,-3}, 得到 {a+1,b-1,c+1,d+2,e+0,f-3} => {b,a,d,f,e,c}
然后再经过一层对应关系C {0,0,1,1,1,-3} => {b,a,e,f,c,d} ,注意第二个关系是把a+0,b+0,c+1,d+1,f+1,e-3,也就是说不管上一个映射怎么变,这一个还是对abcdef进行映射。
那么,这就可以列出来转移映射关系式了: cc[i] = tmp[i]+r[tmp[i]];
再加上偏移:cc[i] = tmp[i]+r[((tmp[i]-st)%m+m)%m]
再对整个做负数处理:cc[i] = ((tmp[i]+r[((tmp[i]-st)%m+m)%m])%m+m)%m
我就是这里没想好,卡了好久。
到这里,题目就做了一大半。
接下来,我们发现A经过三次映射到了D
那么也就是说我们可以把中间的关系合并起来 A -> D。
表示成f(A) = D
那么求反函数即可g(D) = A
接下来就是链表的事情咯。
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
#include <ctime>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); int m,n;
char r[][];
int e[][];
const int MAX_N = **;
int tmp[]; struct CircularNode{
int lst[];
int num;
CircularNode *next;
CircularNode(int n){
memset(lst,,sizeof(lst));
num = n;
next = NULL;
}
~CircularNode(){
if( next ){
delete next;
next = NULL;
}
}
}; struct CircularList{
CircularNode *rt,*cur;
~CircularList(){
delete rt;
}
CircularList(){
rt = NULL;
}
void push_back(int a[],int m){
if( !rt ) {
rt = new CircularNode(m);
cur = rt;
} else {
cur->next = new CircularNode(m);
cur = cur->next;
} for(int j=;j<m;j++){
cur->lst[a[j]] = j;
}
}
}; void mapping(int *r,int st){
int cc[];
for(int i=;i<m;i++){
cc[i] = ((tmp[i]+r[(tmp[i]-st+m)%m])%m+m)%m;
}
for(int i=;i<m;i++){
tmp[i] = cc[i];
}
} int main(){
int kase = ;
while(scanf("%d",&m),m){
if(kase!=) puts("");
printf("Enigma %d:\n",kase++);
for(int i=;i<;i++) scanf("%s",r[i]);
for(int i=;i<;i++){
for(int j=;j<m;j++){
e[i][j] = r[i][j] - 'A' - j;
// printf("%d ",e[i][j]);
}
// puts("");
} CircularList *aCircularList = new CircularList; for(int st1=;st1<m;st1++){
for(int st2=;st2<m;st2++){
for(int st3=;st3<m;st3++){
for(int i=;i<m;i++) tmp[i] = i;
mapping(e[],st3);
// for(int i=0;i<m;i++) printf("%d ",tmp[i]); puts("");
mapping(e[],st2);
// for(int i=0;i<m;i++) printf("%d ",tmp[i]); puts("");
mapping(e[],st1);
// for(int i=0;i<m;i++) printf("%d ",tmp[i]); puts("");
aCircularList->push_back(tmp,m);
// for(int i=0;i<m;i++){
// printf("%d ",aCircularList->cur->lst[i]);
// }
// puts("");
}
// exit(0);
}
} scanf("%d",&n);
getchar();
int kase = ;
while(n--){
CircularNode *p = aCircularList->rt;
char c;
while( (c=getchar())!='\n' ){
// printf("%d\n",p->lst[c-'A']);
putchar('a'+p->lst[c-'A']);
p = p->next;
if(!p) p = aCircularList->rt;
}
puts("");
} delete aCircularList;
}
return ;
} /*
6
FDBCAE
ABDEFC
CDAFEB
1
ACE
*/
[ZOJ 1009] Enigma (模拟)的更多相关文章
- 1009 Enigma
本题的重点是理解清楚题意并能代码模拟.形式是二战德国密码机,和数据结构.算法联系较少. #include <stdio.h> #include <string.h> int m ...
- Enigma模拟-Python
设计思想 Enigma机的机械结构: 键盘:加密人员通过键盘进行输入 转子:Enigma机上一般装有至少3个转轮.每个转轮有代表26个字母的触头和触点,触点和触头在转轮内部有导线相连(一个转轮相当于一 ...
- A - Jugs ZOJ - 1005 (模拟)
题目链接:https://cn.vjudge.net/contest/281037#problem/A 题目大意:给你a,b,n.a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作.让 ...
- ZOJ 2610 Puzzle 模拟
大模拟:枚举6个方向.检查每一个0是否能移动 Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Little Georgie likes ...
- Capture the Flag ZOJ - 3879(模拟题)
In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...
- ZOJ 3705 Applications 模拟
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include< ...
- ZOJ 3652 Maze 模拟,bfs,读题 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4842 要注意题目中两点: 1.在踏入妖怪控制的区域那一刹那,先减行动力,然后才 ...
- ZOJ 1122 Clock(模拟)
Clock Time Limit: 2 Seconds Memory Limit: 65536 KB You are given a standard 12-hour clock with ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
随机推荐
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
关于多线程执行显示进度条的实例! 之前回答了一篇关于怎么在线程中操作进度条的帖子,估计有人看的不是很明白今天没事,写了一个小小的实例,很简单,就2个文件权当抛砖引玉,希望有更好解决方案的人发表一下意见 ...
- python之lxml(xpath)
bs4确实没这个好用,bs4的树太复杂 lxml很好 定位非常好 详细解说在注释里面有了 #!/usr/bin/python3.4 # -*- coding: utf-8 -*- from lxml ...
- 数据库连接工具类——包含取得连接和关闭资源 ConnUtil.java
package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...
- RMAN备份与恢复之不完全恢复
要点:对于RMAN的不完全恢复,有如下步骤: 1)加载数据到mount状态(建议恢复前先做备份) 2)为高并发分配多个通道 3)还原所有(所需)的数据文件 4)使用until time,until s ...
- [算法] 数据结构之AVL树
1 .基本概念 AVL树的复杂程度真是比二叉搜索树高了整整一个数量级——它的原理并不难弄懂,但要把它用代码实现出来还真的有点费脑筋.下面我们来看看: 1.1 AVL树是什么? AVL树本质上还是一棵 ...
- 超强封装的RichTextBox控件(C#源码)
有点类似QQ聊天框所带的RichText. 功能进行了RTF的封装,直接调用函数插入图片,连接,特列文字.具体请查看代码 ExRichTextBox_src
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- 战胜忧虑<5>——运用亚里士多德法则
运用亚里士多德法则 如果人们将忧虑的时间,用来寻找解决问题的答案,那忧虑就会在人们智慧的光芒下消失.那么当你面对忧虑时,应该怎么办理?答案是,我们一定要学会用下面三种分析问题的基本步骤来解决各种不同的 ...
- showdialog窗体不在任务栏显示的问题处理
场景: c#开发的windows窗体用showdialog弹出时,在任务栏中 win7系统显示,win xp和win 2003却不显示. 窗体的ShowInTaskbar已设置为True. 解决: 在 ...
- js工具类 ----正则
function(value){ if(value){ var reg=new RegExp("^[a-zA-Z0-9_-]+$"); return reg.test(v ...