The Imitation Game

《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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值