码迷,mamicode.com
首页 > 其他好文 > 详细

The Imitation Game

时间:2015-02-16 01:39:29      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

《The Imitation Game》是一部非常好的电影,讲述了人工智能之父——阿兰图灵的传奇一生。

重点讲述了他通过破译德国的通讯密码缩短了二战的持续时间,因而拯救了无数生命的伟大事迹。

强烈推荐大家去看一看,这可是豆瓣评分8.6的好片啊!

 

另外,《The Imitation Game》也是当年图灵发表的关于他对人工智能看法的论文。链接在此

 

德国当时的加密方式叫Enigma,具体是怎样的加密机制呢?图灵面临了怎样的挑战呢?

POJ 1449给了我们一次亲身体验破解Enigma的机会。

代码如下:

技术分享
#include <cstdio>
#include <cstring>
using namespace std;

#define D(x) 

int f[6][30];
int g[6][30];
int state[6];
int cipher[90];
int plain[90];
int len;
int *mark[3];
int mark_num;
bool ok;
int power[] = {1, 26, 26 * 26, 26 * 26 * 26};

int get_next(int a[])
{
    char st[90];
    scanf("%s", st);
    for (int i = 0; st[i]; i++)
    {
        if (st[i] == ?)
        {
            a[i] = -1;
            mark[mark_num++] = &a[i];
            continue;
        }
        a[i] = st[i] - a;
    }
    return strlen(st);
}

void input()
{
    mark_num = 0;
    for (int i = 0; i < 5; i++)
    {
        get_next(f[i]);
    }
    get_next(state);
    len = get_next(plain);
    get_next(cipher);
}

void output()
{
    for (int i = 0; i < len; i++)
    {
        putchar(plain[i] + a);
    }
    puts("");
}

int encrypt(int a, int pos)
{
    int temp = f[4][a];
    int cur_state[4];
    int delta[4];

    for (int i = 0; i < 4; i++)
    {
        cur_state[i] = state[i] + pos / power[i];
        cur_state[i] = (cur_state[i] + 26) % 26;
    }
    delta[0] = cur_state[0];
    for (int i = 1; i < 4; i++)
    {
        delta[i] = (cur_state[i] - cur_state[i - 1] + 26) % 26;
    }

    for (int i = 0; i < 4; i++)
    {
        temp = f[i][(temp + delta[i] + 26) % 26];
    }

    for (int i = 2; i >= 0; i--)
    {
        temp = g[i][(temp - delta[i + 1] + 26) % 26];
    }

    temp = g[4][(temp - delta[0] + 26) % 26];
    return temp;
}

bool check()
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            g[i][f[i][j]] = j;
        }
    }

    for (int i = 0; i < len; i++)
    {
        if (encrypt(plain[i], i) != cipher[i])
        {
            return false;
        }
    }
    return true;
}

void dfs(int step)
{
    if (ok)
    {
        return;
    }
    if (step == mark_num)
    {
        if (check())
        {
            ok = true;
            output();
        }
        return;
    }
    for (int i = 0; i < 26; i++)
    {
        *mark[step] = i;
        dfs(step + 1);
    }
}

void test(char a, int b)
{
    check();
    printf("%c %c\n", a, encrypt(a - a, b) + a);
}

int main()
{
    int t;
    scanf("%d", &t);
    for (int i = 1; i <= t; i++)
    {
        printf("Scenario #%d:\n", i);
        input();
        ok = false;
        dfs(0);
        D(test(a, 1));
        puts("");
    }
    return 0;
}
View Code

 

The Imitation Game

标签:

原文地址:http://www.cnblogs.com/rainydays/p/4293636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!