1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
构建前缀树
for word in dictionary:
cur = trie
for ch in word:
if ch not in cur:
cur[ch] = {}
cur = cur[ch]
cur['#'] = {} # word的结束
# 查找
for i, word in enumerate(sentence):
cur = trie
for j, ch in enumerate(word):
if '#' in cur:
find = True
break
if ch not in cur:
break
cur = cur[ch]
|