Last active
August 29, 2015 14:23
-
-
Save oisdk/24a853d92af26049fc27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sliceable where Generator.Element : Equatable, Index.Distance == Int { | |
func lComSubStr< | |
S: Sliceable where | |
S.Generator.Element == Generator.Element, | |
S.Index.Distance == Int | |
>(with: S) -> SubSlice { | |
var (len, end) = (0, 0) | |
let empty = Array(Repeat(count: with.count + 1, repeatedValue: 0)) | |
var mat: [[Int]] = Array(Repeat(count: self.count + 1, repeatedValue: empty)) | |
for (i, sLett) in self.enumerate() { | |
for (j, tLett) in with.enumerate() where tLett == sLett { | |
let curLen = mat[i][j] + 1 | |
mat[i + 1][j + 1] = curLen | |
if curLen > len { | |
len = curLen | |
end = i | |
} | |
} | |
} | |
return self[advance(self.startIndex, (end + 1) - len)...advance(self.startIndex, end)] | |
} | |
} | |
extension Sliceable where Generator.Element : Equatable, SubSlice.Generator.Element == Generator.Element { | |
func contains< | |
C: CollectionType where | |
C.Generator.Element == Generator.Element, | |
Index.Distance == C.Index.Distance | |
> (col: C) -> Bool { | |
return lazy(self.startIndex...advance(self.startIndex, self.count - col.count)) | |
.contains { self[$0..<advance($0, col.count)].elementsEqual(col) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment