POJ1449 & ZOJ1036 Enigma(简单枚举)

本文纯属原创,转载请注明出处。谢谢。

题目传送门:http://poj.org/problem?id=1449

Time Limit: 1000MS   Memory Limit: 10000K
     

Description

During the Second World War, the German military forces mainly used one special machine to secure their communication: the Enigma (see Figure 4). Breaking the Enigma cipher is one of the main success stories of Allied cryptanalysis and the triumph was mainly
attributed to the emergence of digital computation and the genius of the people working at Bletchley Park, the secret cryptanalysis headquarters in England. The reason for this is that, while Enigma is certainly secure against pen and paper attacks, it is
quite easily breakable using digital computers.

Figure 4: An Enigma machine (picture source: http://www.nsa.gov/museum/enigma.html).

The Enigma was a rotor machine, a cipher method which was popular at that time. A rotor is an insulated disk on which electrical contacts, one for each letter of the alphabet, are placed uniformly around the periphery and on each side. An internal conduction
path through the insulating material connects contacts in pairs, one on each side of the disk. An electric current entering on one side travels on an internal path through the rotor cross-section, emerging at one of the contacts on the other side (see Figure
5 for a 3D visualisation of two rotors). Figure 6 shows a schematic side view of the complete rotor system. It shows that the Enigma has three rotors π0, π2 plus an additional reflecting rotor πR.

The input to Enigma is a stream of alphabetic characters without blanks. Every character is subject to the following steps:

1. The plaintext is subject to an initial permutation IP which is implemented by a plugboard.

2. The character resulting from step 1 is sent through the three rotors π0, π2.

3. The resulting character is then sent through the reflecting rotor πR.

4. The character from step 3 is passed back through the rotors π2, π0 (i.e., in the opposite direction).

5. The character from step 4 is subject to the inverse IP-1 of the initial permutation IP.

The interesting point about the use of rotors is that after processing each character, every rotor might be rotated by a certain angle (i.e., a certain amount of letters) before processing the next character. With the Enigma, rotor π0 is rotated by one in anti-clockwise
direction with every new character. When π0 has finished one round (i.e., after processing 26 characters), rotor π1 moves by one character. Similarly, rotor π2 is rotated by one character when π1 has finished one revolution, and the reflecting rotor πR moves
when π2 has finished its rotation. Obviously, πR is the slowest of the four rotors.

The process described above can be used both for encryption and decryption, provided that the permutation πR implemented by the reflecting rotor is an involution. That means πR = πR-1, or, equivalently, ξ=πR(ζ) whenever ζ=πR(ξ). You may assume that
this condition holds.

The secret key of the Enigma consists of (1) the rotors π0, π2, and πR, (2) the plugboard permutation IP, and (3) the initial rotational displacements k0, k1, k2, kR of π0, π2, and πR (see below). The rotors were changed infrequently and were selected from
a set of four possible rotors in the Wehrmacht model.

Problem

You are time-warped to Bletchley Park together with your laptop and should help to decipher some messages which have been intercepted over the day. You are given the entire ciphertext, parts of the plaintext, and parts of the Enigma key. Your task is to determine
the correct key and finally complete the plaintext by decoding the ciphertext.

Input

The first line contains the number of scenarios.

Each scenario begins with the secret key of the Enigma. The secret key is specified by 6 lines. The first four lines contain a specification of the rotors π0, π2 and πR as a sequence of lowercase alphabetic characters. Character i (1<=i<=26) gives the mapping
of the i-th character of the alphabet (e.g., "bha..." means that "a" is mapped to "b", "b" is mapped to "h", "c" is mapped to "a" etc.). Physically, the sequence of characters is given in clockwise direction looking from the front of the rotor stack π0, .
. . , πR. After the rotors follows a similar line giving the plugboard permutation IP. Finally, the sixth line of the key gives the initial displacement k0, k1, k2, kR of the four rotors π0, π2, and πR as a string of four characters where "a" means that the
rotor is in its original position (as defined by the rotor specification above), "b" means that it is rotated by one position in the usual way etc. For example, "dgaa" means that rotor π0 has initial displacement 3, π1 has 6, and π2, πR are both in their original
position.

After the key follow two lines, each containing at least 1 and at most 80 lowercase letters, and no other characters. The first line contains the plaintext while the second line contains the ciphertext. The plaintext and any part of the key may be incomplete,
i.e., some positions in the strings may be question marks "?". The number of question marks in the input will be at most 3.

Output

The output for each scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. In the next line you are to output the completed, decrypted plaintext. You can assume that a solution exists and that it is unique.
Terminate the output for each scenario with a blank line.

Sample Input

2
wfbtiznuvcqejpokshxgmadyrl
hmrgnqpkjcaivwluebfzsyxtdo
druahlbfzvgmwckxpiqysontje
owtvskypjifmluahrqecndbzgx
?bcdefghijklmnopqrstuvwxyz
aaaa
manyorganizationsrelyoncom??ters
grsuztldsznkwnerdpfbovvqnobkyiqn
oqzunvhtxwryfebicmjpklsgda
zupogrskynxtwdfqvbliejcmha
kzvlyjuodmscewxtfbphriqgna
gbcnylaztwkfmdspqvoiurjxeh
rfyhkxbuvplgtqmdiewjosznca
dmeo
???
ave

Sample Output

Scenario #1:
manyorganizationsrelyoncomputers

Scenario #2:
acm

这道题题目长的我这种英语不好的愣是看了半天。

这是2001年XX欧现场赛的题,题目又臭又长,关键其实就是一个十足的水题,关键是网上暂时还找不到这个题的题解,只有赵端阳写的《acm国际大学生程序设计竞赛题解1》里面有提到这道题,但是讲解不够详细,没有解释这个机器的工作原理,所以看了一天多才看懂,然后就毅然决定写一篇来好好解释一下。

大意是这样的:(算了我还是先解释一下这个机器)

首先Enigma作为德国二战时期御用的加密解密器,这个题完完全全地还原了这台天才之作的结构。

它由一个置换盘,3个置换转子盘,和一个反射转子盘组成。

首先是置换盘是一个简单的替换加密器,正向通过时A替换成X,反向通过时X就替换成A,它是固定的,也是最基础最简单的加密方法。《福尔摩斯探案集》里面《跳舞的小人》一节就是用的这个方法,简单地来说就是用26个新的符号替换原来的26个字母,虽然这里替换的26个符号也是取自26个字母,但是它们替换之后就不代表原来的意思了。而反过来就是将26个字符对应替换回原来的字母。

接着最难理解的是3个置换转子,这也是这个机器的精髓。昨天没有看到电路图的时候我一直在想怎么实现的,后来看了一眼电路图瞬间明白了。下面放图。

这是一个置换转子盘的工作原理图。

我们可以看到,无论是明文(左盘)还是密文(右盘),都是按顺序排列而且不变的,变的是中间的转子电路圈。而我们可以看到,置换电路圈的电路是不变的,那么如果一开始A连到D,那么代表的是连在A点的这个电路是往下连3个的,那么一旦转子往下转动一个,不是B连接到了D点,而是B点得到了原来在A点的电路,那么B就会连到往下3个的那个地方,反之亦然。那么每个盘在初始状态的连接情况,就代表着这个盘26个点的电路连接状况,也就是密钥。

如果理解了这个工作原理,那么最后一个反射转子盘就非常好理解了,无非就是去掉了这个转子的右盘,而把左盘上的点两两连在一起,构成了13对,当然转动也会带来配对的改变。

而这四个可以转的盘什么时候转动呢?规则是这样的:每处理完一个字符,第1个转子盘逆时针转一个字符,如果第1个转子盘转了26下,也就是处理完26个字符,第二个转子盘逆时针转动一个字符,然后第二个转26下第三个就转一个字符,第四个亦然。当然我们最开始可以把每个盘都拨动若干个字符,这个拨动不算转动次数。

以上就是对于这个机器的工作原理的解释,也是这个题最核心的地方,只要弄懂了这一点,这个题就是水题了。

题意:现在给你这么一个机器,首先告诉你四个盘的密钥(它对于每条密钥,是按每行26个字符给的,第x行第i个字符代表着字母‘a’+i-1在初始状态下通过第x个盘应该变成什么),接着第五行告诉你那个固定的简单置换盘的对应关系(给的方式同上),接下来第六行给你一行四个字符,第i个字符表示初始状态下,第i个盘被拨动了c[i]-‘a’个字符。接下来第七行给你了一段明文,第八行给你了这段明文在这个机器如上的状态下被加密之后的密文。然后现在告诉你,这上面八行字符串里面有至多3个字符被弄得看不清了,所以读入的时候用‘?’代替了,那么请你找出这三个‘?’应该对应的正确字符后,输出完整的明文(题目保证每组测试数据有且仅有一解)。

那么这个密文在题目中的运行顺序是什么呢:

1、每个字符首先正向通过简单置换板

2、通过步骤1得到的字符再依次正向通过1、2、3这三个转子置换板

3、通过步骤2得到的字符再通过反射转子置换板

4、通过步骤3得到的字符再依次逆向通过3、2、1这三个转子置换板

5、通过步骤4得到的字符再逆向通过简单置换板得到明文

6、每个字符完成上述五步之后,各个转子转动,为下一个字符的处理做准备。

思路:拿到这个题首先分析,只有3个字符不确定,而且每个之后26种情况,所以总共只有26*26*26=17526种可能性,完全可以枚举出来然后一一检测是否能满足解密过程。

于是就愉快地用dfs枚举然后检测了。

下面贴代码。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
#define moo 1000000007//10^9+7
#define PI acos(-1.0)
using namespace std;
char s[10][100];//存最原始的8行字符串
int a[10][100];//存处理后的8行字符串,处理方法是对每个字符-'a'
struct question
{
    int x;
    int y;
}que[5];//存问号在的位置,第X行第Y个字符
int cou;//存问号的数量
int cheek()//检测函数,对于当前枚举的情况,如果解密正确则返回1,否则返回0
{
    int b1[100];
    int b2[100];//将明文密文取出,因为要做修改,所以避免对原数据造成影响。
    int len=strlen(s[7]);
    for(int i=0;i<len;i++)
    {
        b1[i]=a[4][a[7][i]];//将密文第一次通过简单置换板的操作顺手做了
        b2[i]=a[6][i];
    }
    int d[4][30];//存每个转子正向通过时的电路图
    int e[4][30];//存每个转子逆向通过时的电路图
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<26;j++)
        {
            int t=a[i][j];
            d[i][j]=t-j;//正向通过,从j变成t,电路是+(t-j)
            e[i][t]=j-t;//逆向通过,从t变成j,电路是+(j-t)
        }
    }
    int c[4];//存的是每个转子置换板当前的转动情况。
    for(int i=0;i<4;i++)
        c[i]=a[5][i];
    for(int i=0;i<len;i++)
    {
        for(int j=0;j<=3;j++)
        {
            b1[i]+=d[j][(b1[i]+c[j]+26)%26];
            b1[i]=(b1[i]+26)%26;
        }//当前字符依次正向通过1、2、3这三个板,因为反射板工作原理与普通的没什么两样,所以也顺手正向通过
        for(int j=2;j>=0;j--)
        {
            b1[i]+=e[j][(b1[i]+c[j]+26)%26];
            b1[i]=(b1[i]+26)%26;
        }//然后再逆向通过3、2、1这三个板。
        for(int j=0;j<26;j++)//判断这个字符逆向通过简单置换板之后是不是得到明文,如果不是,则当前枚举错误
            if(b1[i]==a[4][j]&&b2[i]!=j)
                return 0;
        c[0]++;//对于每个转子做相应转动,为处理下一个字符做准备
        if(c[0]==a[5][0]+26)
        {
            c[0]=a[5][0];
            c[1]++;
            if(c[1]==a[5][1]+26)
            {
                c[1]=a[5][1];
                c[2]++;
                if(c[2]==a[5][2]+26)
                {
                    c[2]=a[5][2];
                    c[3]++;
                    if(c[3]==a[5][3]+26)
                        c[3]=a[5][3];
                }
            }
        }
    }
    return 1;
}
int dfs(int x)
{
    if(x==cou)
    {
        if(cheek()==1)
            return 1;
        return 0;
    }
    for(int i=0;i<26;i++)//枚举每一位填'a'+i的情况
    {
        a[que[x+1].x][que[x+1].y]=i;
        if(dfs(x+1)==1)
            return 1;
    }
    return 0;
}
void init()
{//预处理八行字符串,把所有点都处理成int型,方便操作,并且把'?'都提取出来
    for(int i=0;i<8;i++)
    {
        int len=strlen(s[i]);
        for(int j=0;j<len;j++)
        {
            if(s[i][j]=='?')
            {
                cou++;
                que[cou].x=i;
                que[cou].y=j;
            }
            else
                a[i][j]=s[i][j]-'a';
        }
    }
}
int main()
{
    int T;
    cin>>T;
    int dd=T;
    while(T--)
    {
        for(int i=0;i<8;i++)
            scanf("%s",s[i]);
        cou=0;
        init();
        dfs(0);
        printf("Scenario #%d:\n",dd-T);
        int len=strlen(s[6]);
        for(int i=0;i<len;i++)
            printf("%c",a[6][i]+'a');
        printf("\n\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-01 18:27:09

POJ1449 & ZOJ1036 Enigma(简单枚举)的相关文章

简单除法(简单枚举优化)

#include<iostream> #include<algorithm> using namespace std; void panduan(int s,int k) { int n,m;bool l=1; n=s;m=k; int i,sn=0,a[20],j; for(i=0;n!=0;i++) { a[i]=n%10; n=n/10; } for(;m!=0;i++) { a[i]=m%10; m=m/10; } i--; sort(a,a+i); if(i==8) {a

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

UVA - 10167 - Birthday Cake (简单枚举)

思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int x[105], y[105]; int main() { int A, B, N; while(scanf("%d", &N), N) { for(int

Java练习 SDUT-1959_简单枚举类型——植物与颜色

简单枚举类型--植物与颜色 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请定义具有red, orange, yellow, green, blue, violet六种颜色的枚举类型color,根据输入的颜色名称,输出以下六种植物花朵的颜色: Rose(red), Poppies(orange), Sunflower(yellow), Grass(green), Bluebells(blue), Violets(v

hdu5073 简单枚举+精度处理

其实这题还是挺简单的,因为移动k个星球后,这k个星球的权值就可以变为0,所以只有剩下的本来就是连着的才是最优解,也就是说要动也是动两端的,那么就O(N)枚举一遍动哪些就好了. 我是在杭电oj题目重现的比赛上做这题,因为之前听人说现场赛时有人用n^2的算法蹭过了,所以我不断蹭,蹭了一个小时都没蹭过...~!@#¥%…… 先贴一份乱七八糟想蹭过的代码 /* * Author : ben */ #include <cstdio> #include <cstdlib> #include &

UVA 725 UVA 10976 简单枚举

UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件.(注意前导零的判断) 枚举的方法为 for(int i=1234;i<=100000/n;i++){} #include<cstdio> #include<cstring> int num[10]; bool check(int a,int b) { memset(num,0,

简单枚举---从一数组中任取n个元素

这个问题很明显的需要用枚举的思想来一一遍历数组元素并进行无重复组合 给出一个数组a[14]={1,2,3,4,5,6,7,8,9,10,11,12,13,14},求从中任取n个元素的所有组合 如果任取1个元素+取所有元素:15种可能 如果任取2个元素:C^2 14=91  递归组合 1 2    1 3    1 4  1 5   1 6 1 7 1 8 1 9 1 10 1 11 1 12   1 13   1 14   14种 2 3   2 4  2 5  2  6   ... 13种 1

hdu5258简单枚举

百度之星复赛第一题.不明白这么水的题为何一堆人没过...这些人是咋晋级复赛的呢... /* * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <queue

UVA 10976 Fractions Again?! 简单枚举题

#include<stdio.h> struct pairs{ int x,y; }; struct pairs pairs[10000]; int judge(int k,int i){ if((k*i)%(i-k)==0) return 1; else return 0; } int main(){ int k,count; while(scanf("%d",&k)!=EOF){ count=0; for(int i=k+1;i<=2*k;i++){ if