Skip to content

Instantly share code, notes, and snippets.

@codeb2cc
Created June 26, 2014 18:56
Show Gist options
  • Save codeb2cc/f45b1e257f3a38807d17 to your computer and use it in GitHub Desktop.
Save codeb2cc/f45b1e257f3a38807d17 to your computer and use it in GitHub Desktop.
Generalized suffix tree implemented in suffix array
package main
import (
"fmt"
"index/suffixarray"
"regexp"
"strings"
)
func main() {
words := []string{
"aardvark",
"happy",
"hello",
"hero",
"he",
"hotel",
"hello world",
}
// use \x00 to start each string
joinedStrings := strings.Join(words, "\x00") + "\x00"
sa := suffixarray.New([]byte(joinedStrings))
// User has typed in "he"
match, err := regexp.Compile("[^\x00]*ell[^\x00]*")
if err != nil {
panic(err)
}
ms := sa.FindAllIndex(match, -1)
for _, m := range ms {
start, end := m[0], m[1]
fmt.Printf("match = %q\n", joinedStrings[start:end])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment