lcp

package module
v0.0.0-...-745a8e8 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2019 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-lcp

Effectively search the longest common prefix length for two specified suffixes of the given string.

Takes O(N * log(N)) for preprocessing and O(log(N)) on searching (N is length of the string).

Usage example (play.golang.org)

package main

import (
	"fmt"
	lcppkg "github.com/NikitkoCent/go-lcp"
)

func main() {
	const word = "abacaba"

	lcp := lcppkg.NewLongestCommonPrefix(word) // O(N * log N)

	// Suffix at 0 offset (first parameter) is 'abacaba'
	// Suffix at 4 offset (second parameter) is 'aba'
	// Their longest common prefix length is 3
	// So result = 3 will be printed
	fmt.Println(lcp.Get(0, 4))                 // O(log N)

	// 6 will be printed
	fmt.Println(lcp.Get(1, 1))

	// The next string will cause panic because one of parameters is out of bounds
	// fmt.Println(lcp.Get(0, 7))
}

Documentation

Overview

Package lcp provides data structure which allows effectively calculate longest common prefix of two any suffixes of specified string.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LongestCommonPrefix

type LongestCommonPrefix interface {

	// Get the longest common prefix length for two specified suffixes of the string
	//
	// Arguments:
	//   firstSuffixIndex - position for the first suffix
	//   secondSuffixIndex - position for the second suffix
	//
	// Return value: position of the longest common prefix for specified suffixes
	//
	// Complexity: O(logN), where N is the string length
	Get(firstSuffixIndex uint, secondSuffixIndex uint) uint
}

func NewLongestCommonPrefix

func NewLongestCommonPrefix(str string) LongestCommonPrefix

Create longest common prefix data structure for the specified string

Arguments:

string - the string, for suffixes of which longest common prefix will be calculated

Complexity: O(N * logN), where N is the string length

Jump to

Keyboard shortcuts

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