-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstrings.py
More file actions
33 lines (31 loc) · 855 Bytes
/
substrings.py
File metadata and controls
33 lines (31 loc) · 855 Bytes
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
'''
求两个字符串的最长公共子串
'''
# 先生成一个二维列表
def substrings():
a = "abcdea"
b = 'abcde'
lst1 = len(a)
lst2 = len(b)
max = 0 # 最大值,默认为0
p = 0
record = [[0 for i in range(lst1+1)] for j in range(lst2+1)]
# print(record)
for i in range(lst1):
for j in range(lst2):
# record[0][i+1] = a[i]
# record[j+1][0] = b[j]
# record[0][0] = 0
if a[i] == b[j]:
print(i)
record[j+1][i+1] = record[j][i] + 1
if record[j+1][i+1] > max:
max = record[j+1][i+1]
p = i + 1
print(*record, sep='\n')
print(a[p-max:p])
# print(lst.append(*record))
# print(*record, sep = "\n")
# print(lst)
if __name__ == '__main__':
substrings()