The Imitation Game

时间:2023-03-09 02:45:02
The Imitation Game

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[][];
int g[][];
int state[];
int cipher[];
int plain[];
int len;
int *mark[];
int mark_num;
bool ok;
int power[] = {, , * , * * }; int get_next(int a[])
{
char st[];
scanf("%s", st);
for (int i = ; st[i]; i++)
{
if (st[i] == '?')
{
a[i] = -;
mark[mark_num++] = &a[i];
continue;
}
a[i] = st[i] - 'a';
}
return strlen(st);
} void input()
{
mark_num = ;
for (int i = ; i < ; i++)
{
get_next(f[i]);
}
get_next(state);
len = get_next(plain);
get_next(cipher);
} void output()
{
for (int i = ; i < len; i++)
{
putchar(plain[i] + 'a');
}
puts("");
} int encrypt(int a, int pos)
{
int temp = f[][a];
int cur_state[];
int delta[]; for (int i = ; i < ; i++)
{
cur_state[i] = state[i] + pos / power[i];
cur_state[i] = (cur_state[i] + ) % ;
}
delta[] = cur_state[];
for (int i = ; i < ; i++)
{
delta[i] = (cur_state[i] - cur_state[i - ] + ) % ;
} for (int i = ; i < ; i++)
{
temp = f[i][(temp + delta[i] + ) % ];
} for (int i = ; i >= ; i--)
{
temp = g[i][(temp - delta[i + ] + ) % ];
} temp = g[][(temp - delta[] + ) % ];
return temp;
} bool check()
{
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
g[i][f[i][j]] = j;
}
} for (int i = ; 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 = ; i < ; i++)
{
*mark[step] = i;
dfs(step + );
}
} 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 = ; i <= t; i++)
{
printf("Scenario #%d:\n", i);
input();
ok = false;
dfs();
D(test('a', ));
puts("");
}
return ;
}