forked from Moonshile/ChineseWordSegmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.py
More file actions
89 lines (82 loc) · 2.15 KB
/
sequence.py
File metadata and controls
89 lines (82 loc) · 2.15 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#coding=utf-8
"""
Algorithms for sequences
Author: 段凯强
"""
def dedup(ls):
"""
deduplicate the given SORTED list
"""
i, j = 0, 0
while j < len(ls):
if ls[j] == ls[i]:
j += 1
else:
i += 1
ls[i] = ls[j]
return ls[:i + 1]
def genSubstr(string, n):
"""
Generate all substrings of max length n for string
"""
length = len(string)
res = []
for i in range(0, length):
for j in range(i + 1, min(i + n + 1, length + 1)):
res.append(string[i: j])
return res
def genSubparts(string):
"""
Partition a string into all possible two parts, e.g.
given "abcd", generate [("a", "bcd"), ("ab", "cd"), ("abc", "d")]
For string of length 1, return empty list
"""
length = len(string)
res = []
for i in range(1, length):
res.append((string[0:i], string[i:]))
return res
def longestSubsequenceLength(s1, s2):
n = len(s2) + 1
cur = [0]*n
next = [0]*n
tmp = None
for i in s1:
for j in range(0, n):
if j == 0: next[j] = 0
else: next[j] = cur[j - 1] + 1 if i == s2[j - 1] else max(next[j - 1], cur[j])
tmp = next
next = cur
cur = tmp
return cur[n - 1]
def longestSubsequence(s1, s2):
n = len(s2) + 1
cur = [0]*n
next = [0]*n
tmp = None
__NONE, __UP, __LEFT, __NEW = 0, 1, 2, 3
orientation = [[__NONE]*n]
for i in s1:
ori = []
for j in range(0, n):
if j == 0:
next[j] = 0
ori.append(__NONE)
else:
next[j] = cur[j - 1] + 1 if i == s2[j - 1] else max(next[j - 1], cur[j])
ori.append(__NEW if i == s2[j - 1] else (__LEFT if next[j - 1] > cur [j] else __UP))
orientation.append(ori)
tmp = next
next = cur
cur = tmp
i, j, res = len(s1), n - 1, ''
ori = orientation[i][j]
while ori != __NONE:
if ori == __UP: i -= 1
elif ori == __LEFT: j -= 1
elif ori == __NEW:
i -= 1
j -= 1
res += s2[j]
ori = orientation[i][j]
return res[::-1]