The Chasm | Think Like A Coder, Ep 6

449,226 views ・ 2020-01-30

TED-Ed


请双击下面的英文字幕来播放视频。

翻译人员: Yizhuo He 校对人员: Yanyan Hong
[ 像程序员一样思考 ]
[ 地点:198 森林 ]
[ 第六集 峡谷 ]
00:21
Ethic, Hedge, and Octavia stand on the edge of a bottomless ravine.
0
21937
4675
艾斯克、海吉和奥克塔维亚 站在无底峡谷的边缘。
00:26
It’s the only thing between them and the tower
1
26612
2690
峡谷是他们与塔之间的唯一阻碍,
00:29
that houses the second of three powerful artifacts.
2
29302
3648
塔里藏有三个强大神器中的第二个。
00:32
They’ve got a brief window of time to get across before the guards return.
3
32950
4980
他们在警卫回来前
只有一小段时间可以跨越这个峡谷。
00:37
With Hedge’s fuel gauge on empty he won’t be able to fly Ethic across,
4
37930
4705
海吉的燃料箱空了, 无法带艾斯克飞越过去,
00:42
so the only option is to make a bridge.
5
42635
3630
所以,唯一选择是建一座桥。
00:46
Fortunately, the floating stacks of stones nearby are bridge components—
6
46265
4655
幸运的是,附近漂浮的石头堆 可以用来搭一座桥,
00:50
invented by Octavia herself— called hover-blocks.
7
50920
4026
石头堆是奥克塔维亚 自己发明的——
叫作“飘浮块”。
00:54
Activate a pile with a burst of energy,
8
54946
2552
他们可以用一股能量 激活一堆飘浮块,
00:57
and they’ll self-assemble to span the ravine as Ethic walks across.
9
57498
4516
这样在这些飘浮块自动组装时, 艾斯克就能跨越峡谷。
01:02
But there is, of course, a catch.
10
62014
3578
但现在存在一个问题。
01:05
The hover-blocks are only stable when they’re perfectly palindromic.
11
65592
4659
这些飘浮块只有在组合成 完美的回文结构时才能保持稳定。
01:10
Meaning they have to form a sequence
12
70251
2260
也就是说,它们得被组合成
01:12
that’s the same when viewed forwards and backwards.
13
72511
4263
正序看和倒序看都一致的结构才行。
01:16
The stacks start in random orders,
14
76774
2170
飘浮块最初是随机排列的,
01:18
but will always put themselves into a palindromic configuration
15
78944
3660
但如果可行,
它们随后会自动拼装成回文结构。
01:22
if they can.
16
82604
1290
01:23
If they get to a point where a palindrome isn’t possible,
17
83894
2880
但如果它们无法被拼成回文结构,
01:26
the bridge will collapse,
18
86774
1551
桥就会坍塌掉,
01:28
and whoever’s on it will fall into the ravine.
19
88325
3489
走在上面的人就会坠入深谷。
01:31
Let’s look at an example.
20
91814
1638
让我们来看一个例子。
01:33
This stack would make itself stable.
21
93452
2460
这堆飘浮块能被组成稳定的结构。
01:35
First the A blocks hold themselves in place.
22
95912
3000
首先,A 型方块 能被排列在左右两侧。
01:38
Then the B’s.
23
98912
1070
接着是 B 型方块。
01:39
And finally the C would nestle right between the B’s.
24
99982
3690
最后,C 型方块正好 能被放在两块 B 之间。
01:43
However, suppose there was one more A.
25
103672
3450
但假设多一块 A 型方块的话。
01:47
First two A blocks form up, then two B’s,
26
107122
3120
首先两块 A 排列好, 然后两块 B 也排列好,
01:50
but now the remaining C and A have nowhere to go,
27
110242
3370
但剩下的 A 块 和 C 块就没地方放了,
01:53
so the whole thing falls apart.
28
113612
2460
因此整座桥都会塌掉。
01:56
The Node of Power enables Hedge to energize a single stack of blocks.
29
116072
4670
力量节点晶石只够海吉 激活一堆飘浮块,
02:00
What instructions can Ethic give Hedge to allow him to efficiently find
30
120742
4334
艾斯克要给海吉什么指令,
才能让它有效地找到并激活 一堆稳固且为回文结构的飘浮块呢?
02:05
and power a stable palindromic stack?
31
125076
3051
02:08
Pause now to figure it out for yourself.
32
128127
9970
[ 可暂停播放自行解题 ]
02:18
Examples of palindromes include ANNA, RACECAR, and MADAM IM ADAM.
33
138097
5461
回文排列的例子如:ANNA、RACECAR、 以及 MADAM IM ADAM。
02:23
Counting the number of times a given letter appears in a palindrome
34
143558
3730
通过数一个字母 在回文中出现的次数,
02:27
will reveal a helpful pattern.
35
147288
2532
你会发现一个有用的模式。
02:29
Pause now to figure it out for yourself.
36
149820
4831
[ 可暂停播放自行解题 ]
02:34
Let’s first look at a naïve solution to this problem.
37
154651
3490
我们先来看一个简单的方案。
02:38
A naïve solution is a simple, brute-force approach that isn’t optimized—
38
158141
4708
简易方案是一种
简单的、未被优化的粗暴方法,
02:42
but will get the job done.
39
162849
1980
但是它可以达成目标。
02:44
Naïve solutions are helpful ways to analyze problems,
40
164829
3491
简单方案是分析问题的有用方法,
02:48
and work as stepping stones to better solutions.
41
168320
3434
是更优方案的铺路石。
02:51
In this case, a naïve solution is to approach a pile of blocks,
42
171754
3770
在这种情况下,一个简单方案是:
尝试这堆飘浮块的所有可能组合,
02:55
try all the arrangements,
43
175524
1500
02:57
and see if one is a palindrome by reading it forward and then backwards.
44
177024
4727
并通过正向,反向阅读 来确定其是否为回文结构。
03:01
The problem with this approach
45
181751
1480
但这种方法有个缺陷,
03:03
is that it would take a tremendous amount of time.
46
183231
2493
它需要大量的时间。
03:05
If Hedge tried one combination every second,
47
185724
2850
如果海吉每秒 能判断一种组合可不可行,
03:08
a stack of just 10 different blocks would take him 42 days to exhaust.
48
188574
5198
那么只有 10 块的飘浮块堆, 需要 42 天才能算完。
03:13
That’s because the total time is a function of the factorial
49
193772
3830
这是因为总时间
是总块数的阶乘。
03:17
of the number of blocks there are.
50
197602
2142
03:19
10 blocks have over 3 million combinations.
51
199744
3600
10 个飘浮块就有 超过 300 万种组合方式。
03:23
What this naïve solution shows is that we need a much faster way
52
203344
4280
这个简单的解决方案表明, 我们需要一种更快的方法
03:27
to tell whether a pile of blocks can form a palindrome.
53
207624
3593
来判断一堆飘浮块 是否可以形成回文。
03:31
To start, it may be intuitively clear that a pile of all different blocks
54
211217
4716
首先,很明显的是, 一堆各不相同的飘浮块
03:35
will never form one.
55
215933
1420
永远不可能形成回文结构。
03:37
Why?
56
217353
790
为什么?
03:38
The first and last blocks can’t be the same if there are no repeats.
57
218143
5281
因为如果没有重复,
第一块和最后一块就不可能相同。
03:43
So when can a given sequence become a palindrome?
58
223424
5012
那么什么时候一个给定的 序列可以形成回文呢?
03:48
One way to figure that out is to analyze a few existing palindromes.
59
228436
4480
一种方法是分析 一些现有的回文。
03:52
In ANNA, there are 2 A’s and 2 N’s.
60
232916
3254
ANNA 有两个 A 和两个 N,
03:56
RACECAR has 2 R’s, 2 A’s, 2 C’s, and 1 E.
61
236170
4886
RACECAR 有两个 R、 两个 A、两个 C 和一个 E,
04:01
And MADAM IM ADAM has 4 M’s, 4 A’s, 2 D’s, and 1 I.
62
241056
6730
MADAM IM ADAM 中, 有四个 M、四个 A、两个 D 和 一个 I。
04:07
The pattern here is that most of the letters occur
63
247786
3140
模式是,大多数字母
04:10
an even number of times,
64
250926
1774
会出现偶数次,
04:12
and there’s at most 1 that occurs just once.
65
252700
3280
最多只有一个字母 可以只出现一次。
04:15
Is that it?
66
255980
1110
这就完了吗?
04:17
What if RACECAR had 3 E’s instead of 1?
67
257090
3260
如果 RACECAR 有3个 E 而不是1个呢?
04:20
We could tack the new E’s onto the ends and still get a palindrome,
68
260350
3710
我们可以把新的两个 E 钉在两端, 仍能得到一个回文,
04:24
so 3 is ok.
69
264060
1840
所以字母出现 3 次是可以的。
04:25
But make that 3 E’s and 3 C’s, and there’s nowhere for the last C to go.
70
265900
6064
但如果有 3 个 E 和 3 个 C , 最后一个 C 就没有地方放了。
04:31
So the most generalized insight is that
71
271964
2720
让我们来概括一下思路,
04:34
at most one letter can appear an odd number of times,
72
274684
4096
最多只有一个字母能出现奇数次,
04:38
but the rest have to be even.
73
278780
3066
但其他字母必须出现偶数次。
04:41
Hedge can count the letters in each stack and organize them into a dictionary,
74
281846
4314
海吉可以数每个字母 在飘浮堆中出现的次数,
把这些信息存放到 字典这种数据结构中,
04:46
which is a tidy way of storing information.
75
286160
2725
这是一种储存信息的简洁方式。
04:48
A loop could then go through and count how many times odd numbers appear.
76
288885
4579
可以通过一个循环遍历这个字典, 数有几个字母出现了奇数次。
04:53
If there are less than 2 odd characters, the stack can be made into a palindrome.
77
293464
5500
如果有少于两个字母出现了奇数次,
那么这堆飘浮块 就能被组成回文结构。
04:58
This approach is much, much faster than the naïve solution.
78
298964
3720
这种方法比之前的 那个简单解决方案要快得多。
05:02
Instead of factorial time, it takes linear time.
79
302684
3420
它只需线性时间, 而不是阶乘时间。
05:06
That’s where the time increases
80
306104
1570
线性时间指的是
05:07
in proportion to the number of blocks there are.
81
307674
2710
程序运行时间与方块数量成正比。
05:10
Now write a loop for Hedge to approach the piles individually,
82
310384
3990
现在,请你为海吉编写一个循环, 来分别处理这些飘浮块堆,
05:14
and stop when he finds a good one, and you’ll be ready to go.
83
314374
4155
当它找到可行的方案时, 暂停程序,这样就成功了。
05:18
Here’s what happens:
84
318529
1389
接下来会发生的是:
05:19
Hedge is fast, but there are so many piles it takes a long time.
85
319918
4046
虽然海吉的速度很快,
但有很多的飘浮块堆, 要花很长的时间,
05:23
Too long.
86
323964
1356
很长很长的时间。
06:17
Ethic and Hedge are safe.
87
377897
1680
艾斯克和海吉现在安全了,
06:19
But Octavia is not so lucky.
88
379577
2423
但奥克塔维亚就没那么幸运了。
关于本网站

这个网站将向你介绍对学习英语有用的YouTube视频。你将看到来自世界各地的一流教师教授的英语课程。双击每个视频页面上显示的英文字幕,即可从那里播放视频。字幕会随着视频的播放而同步滚动。如果你有任何意见或要求,请使用此联系表与我们联系。

https://forms.gle/WvT1wiN1qDtmnspy7