FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
snippet-code/PythonNote/Alg-LongestCommonString.py at master · intergret/snippet-code · GitHub
intergret
/
snippet-code
Public
Notifications
You must be signed in to change notification settings
Fork
75
Star
56
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
snippet-code
/
PythonNote
/
Alg-LongestCommonString.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (35 loc) · 1.12 KB
Breadcrumbs
snippet-code
/
PythonNote
/
Alg-LongestCommonString.py
Copy path
File metadata and controls
43 lines (35 loc) · 1.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# -*- coding: utf-8 -*-
# 题目:求两字符串的最初公共子序列
martix
=
[]
def
computeLCS
(
strX
,
strY
):
global
martix
for
time
in
range
(
len
(
strY
)
+
1
):
martix
.
append
([
0
]
*
(
len
(
strX
)
+
1
))
for
oneStrIdx
in
range
(
len
(
strY
)):
for
anotherStrIdx
in
range
(
len
(
strX
)):
if
strY
[
oneStrIdx
]
==
strX
[
anotherStrIdx
]:
martix
[
oneStrIdx
+
1
][
anotherStrIdx
+
1
]
=
martix
[
oneStrIdx
][
anotherStrIdx
]
+
1
else
:
martix
[
oneStrIdx
+
1
][
anotherStrIdx
+
1
]
=
max
(
martix
[
oneStrIdx
+
1
][
anotherStrIdx
],
martix
[
oneStrIdx
][
anotherStrIdx
+
1
])
def
getAllLCS
(
OneLCS
,
pointY
,
pointX
):
global
martix
tempLCS
=
[
element
for
element
in
OneLCS
]
if
martix
[
pointY
][
pointX
]
!=
0
:
if
strY
[
pointY
-
1
]
==
strX
[
pointX
-
1
]:
tempLCS
.
append
(
strX
[
pointX
-
1
])
getAllLCS
(
tempLCS
,
pointY
-
1
,
pointX
-
1
)
else
:
if
martix
[
pointY
][
pointX
]
==
martix
[
pointY
][
pointX
-
1
]:
getAllLCS
(
tempLCS
,
pointY
,
pointX
-
1
)
if
martix
[
pointY
][
pointX
]
==
martix
[
pointY
-
1
][
pointX
]:
getAllLCS
(
tempLCS
,
pointY
-
1
,
pointX
)
else
:
OneLCS
.
reverse
()
print
OneLCS
if
__name__
==
'__main__'
:
strX
=
'abcbdab'
strY
=
'bdcaba'
# strX='inthebegining'
# strY='allthingsarelost'
computeLCS
(
strX
,
strY
)
getAllLCS
([],
len
(
strY
),
len
(
strX
))
Back
|
FazBrowse Home
|
New Git URL