Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,13 @@
|
|||
rlcs(_ s1: String, _ s2: String) -> String {
|
||||
if s1.count == 0 || s2.count == 0 {
|
||||
return ""
|
||||
} else if s1[s1.index(s1.endIndex, offsetBy: -1)] == s2[s2.index(s2.endIndex, offsetBy: -1)] {
|
||||
return rlcs(String(s1[s1.startIndex..<s1.index(s1.endIndex, offsetBy: -1)]),
|
||||
String(s2[s2.startIndex..<s2.index(s2.endIndex, offsetBy: -1)])) + String(s1[s1.index(s1.endIndex, offsetBy: -1)])
|
||||
} else {
|
||||
let str1 = rlcs(s1, String(s2[s2.startIndex..<s2.index(s2.endIndex, offsetBy: -1)]))
|
||||
let str2 = rlcs(String(s1[s1.startIndex..<s1.index(s1.endIndex, offsetBy: -1)]), s2)
|
||||
|
||||
return str1.count > str2.count ? str1 : str2
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
func lcs(_ s1: String, _ s2: String) -> String {
|
||||
var lens = Array(
|
||||
repeating:Array(repeating: 0, count: s2.count + 1),
|
||||
count: s1.count + 1
|
||||
)
|
||||
|
||||
for i in 0..<s1.count {
|
||||
for j in 0..<s2.count {
|
||||
if s1[s1.index(s1.startIndex, offsetBy: i)] == s2[s2.index(s2.startIndex, offsetBy: j)] {
|
||||
lens[i + 1][j + 1] = lens[i][j] + 1
|
||||
} else {
|
||||
lens[i + 1][j + 1] = max(lens[i + 1][j], lens[i][j + 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var returnStr = ""
|
||||
var x = s1.count
|
||||
var y = s2.count
|
||||
while x != 0 && y != 0 {
|
||||
if lens[x][y] == lens[x - 1][y] {
|
||||
x -= 1
|
||||
} else if lens[x][y] == lens[x][y - 1] {
|
||||
y -= 1
|
||||
} else {
|
||||
returnStr += String(s1[s1.index(s1.startIndex, offsetBy: x - 1)])
|
||||
x -= 1
|
||||
y -= 1
|
||||
}
|
||||
}
|
||||
|
||||
return String(returnStr.reversed())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue