Skip to content

Instantly share code, notes, and snippets.

@andot
Created September 30, 2014 06:52
Show Gist options
  • Save andot/a5a71f36365284a3de9e to your computer and use it in GitHub Desktop.
Save andot/a5a71f36365284a3de9e to your computer and use it in GitHub Desktop.
给定一个query和一个text,均由小写字母组成。要求在text中找出以同样的顺序连续出现在query中的最长连续字母序列的长度。例如,query为"acbac",text为"acaccbabb",那么text中的"cba"为最长的连续出现在query中的字母序列,因此,返回结果应该为其长度3。
package main
import (
"fmt"
"strings"
)
func maxlen(query string, text string) (max int) {
max = 0
p := 0
textlen := len(text)
querylen := len(query)
end := p + max + 1
for (end < textlen) && (max < querylen) {
s := text[p:end]
if strings.Contains(query, s) {
max = max + 1
} else {
p = p + 1
}
end = p + max + 1
}
return max
}
func main() {
fmt.Println(maxlen("accbb", "acaccbabb"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment