2015-12-30 11:13:43 +03:00
|
|
|
// Copyright (c) 2014-2016, b3log.org & hacpai.com
|
2014-12-07 06:42:34 +03:00
|
|
|
//
|
2014-11-13 12:12:02 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-12-07 06:42:34 +03:00
|
|
|
//
|
2014-11-13 12:12:02 +03:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-07 06:42:34 +03:00
|
|
|
//
|
2014-11-13 12:12:02 +03:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-11-13 12:00:29 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
type str struct{}
|
|
|
|
|
|
|
|
// String utilities.
|
|
|
|
var Str = str{}
|
|
|
|
|
|
|
|
// Contains determines whether the str is in the strs.
|
|
|
|
func (*str) Contains(str string, strs []string) bool {
|
|
|
|
for _, v := range strs {
|
|
|
|
if v == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// LCS gets the longest common substring of s1 and s2.
|
|
|
|
//
|
|
|
|
// Refers to http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring.
|
|
|
|
func (*str) LCS(s1 string, s2 string) string {
|
|
|
|
var m = make([][]int, 1+len(s1))
|
|
|
|
|
|
|
|
for i := 0; i < len(m); i++ {
|
|
|
|
m[i] = make([]int, 1+len(s2))
|
|
|
|
}
|
|
|
|
|
|
|
|
longest := 0
|
2014-12-07 06:42:34 +03:00
|
|
|
xLongest := 0
|
2014-11-13 12:00:29 +03:00
|
|
|
|
|
|
|
for x := 1; x < 1+len(s1); x++ {
|
|
|
|
for y := 1; y < 1+len(s2); y++ {
|
|
|
|
if s1[x-1] == s2[y-1] {
|
|
|
|
m[x][y] = m[x-1][y-1] + 1
|
|
|
|
if m[x][y] > longest {
|
|
|
|
longest = m[x][y]
|
2014-12-07 06:42:34 +03:00
|
|
|
xLongest = x
|
2014-11-13 12:00:29 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m[x][y] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 06:42:34 +03:00
|
|
|
return s1[xLongest-longest : xLongest]
|
2014-11-13 12:00:29 +03:00
|
|
|
}
|