strings

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

子字符串查找

package main

import (
	"fmt"
	"github.com/howz97/algorithm/strings"
	"os"
)

func main() {
	file, err := os.Open("../strings/tale.txt")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	fileStat, err := file.Stat()
	if err != nil {
		panic(err)
	}
	txt := make([]byte, fileStat.Size())
	_, err = file.Read(txt)
	if err != nil {
		panic(err)
	}

	pattern := "It is a far, far better thing that I do, than I have ever done"
	searcher := strings.NewKMP(pattern)
	//searcher := strings.NewBM(pattern)
	i := searcher.Index(string(txt))
	//i := strings.IndexRabinKarp(string(txt), pattern)
	fmt.Println(string(txt[i-50 : i+100]))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IndexRabinKarp

func IndexRabinKarp(s, substr string) int

Types

type BoyerMoore

type BoyerMoore struct {
	// contains filtered or unexported fields
}

func NewBoyerMoore

func NewBoyerMoore(pattern string) *BoyerMoore

func (*BoyerMoore) Index

func (bm *BoyerMoore) Index(s string) int

func (*BoyerMoore) IndexAll

func (bm *BoyerMoore) IndexAll(s string) (indices []int)

func (*BoyerMoore) LenP

func (bm *BoyerMoore) LenP() int

type KMP

type KMP struct {
	// contains filtered or unexported fields
}

func NewKMP

func NewKMP(pattern string) *KMP

func (*KMP) Index

func (kmp *KMP) Index(s string) int

func (*KMP) IndexAll

func (kmp *KMP) IndexAll(s string) (indices []int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL