Created
June 26, 2014 18:56
-
-
Save codeb2cc/f45b1e257f3a38807d17 to your computer and use it in GitHub Desktop.
Generalized suffix tree implemented in suffix array
This file contains hidden or 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
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