-
Notifications
You must be signed in to change notification settings - Fork 0
/
30.py
63 lines (56 loc) · 1.45 KB
/
30.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class ListNode(object):
def __init__(self, x):
self.val = x
self.maxval = x
self.fail = None
self.letter = [0 for i in range(0,26)]
def insert(head, word, success):
x = head
for i in word:
if (x.letter[ord(i)-ord('a')] == 0):
p = ListNode(0)
x.letter[ord(i)-ord('a')] = p
x = x.letter[ord(i)-ord('a')]
x.val += 1
x.maxval += 1
success += x
def reset(success):
for x in success:
x.val = x.maxval
def set_fail(head):
head.fail = head
queue = [head]
while (queue != []):
x = queue[0]
for i in range(0,26):
if (x.letter[i] != 0):
queue += x.letter[i]
if (x.fail.letter[i] != 0):
x.letter[i].fail = x.fail.letter[i]
else :
x.letter[i].fail = head
if (len(queue) <= 1):
break
else:
x = queue[1:]
def findSubstring(s, words):
head = ListNode(0)
success = []
for i in words:
insert(head, i, success)
set_fail(head)
x = head
i = 0
l = len(s)
while (i < l):
if (x.letter[ord(s[i])] != 0):
i += 1
x = x.letter[ord(s[i])]
if ()
else:
return 0
a = ListNode(0)
b = ListNode(2)
a.letter[21] = b
print(a.letter[21].val)
print(ord('z')-ord('a'))