"Longest Duplicate Substring" hard task No 1044 not accepted yet, experimenting

This commit is contained in:
2022-05-21 23:59:27 +03:00
parent 4d4cc6758d
commit 729f686164
2 changed files with 162 additions and 2 deletions

View File

@@ -1,5 +1,7 @@
package main
import "sync"
func LongestDupSubstringBruteforce(s string) string {
strLen := len(s)
if strLen < 2 {
@@ -135,6 +137,158 @@ func LongestDupSubstringSmartforceMark2(s string) string {
return res
}
func LongestDupSubstring(s string) string {
return LongestDupSubstringSmartforceMark2(s)
func LongestDupSubstringBruteforceReversedMark2(s string) string {
strLen := len(s)
if strLen < 2 {
return ""
}
res := ""
for ln := strLen - 1; ln > 0; ln-- {
if ln < len(res) {
break
}
for i := 0; i < strLen-ln; i++ {
word1 := s[i : i+ln]
for j := i + 1; j+ln <= strLen; j++ {
word2 := s[j : j+ln]
if word1 == word2 {
if len(res) < len(word1) {
res = word1
}
break
}
}
}
}
return res
}
func LongestDupSubstringSmartforceMark3(s string) string {
strLen := len(s)
if strLen < 2 {
return ""
}
indicesNext := []int{}
indicesAll := []int{}
for j := 0; j < strLen; j++ {
indicesAll = append(indicesAll, j)
}
res := ""
for i := 0; i < strLen-len(res); i++ {
indicesCurrent := indicesAll[i+1:]
for ln := 1; ln <= strLen-i; ln++ {
word1 := s[i : i+ln]
for _, index := range indicesCurrent {
if index+ln > strLen {
continue
}
if s[i+ln-1] == s[index+ln-1] {
if len(res) < len(word1) {
res = word1
}
indicesNext = append(indicesNext, index)
}
}
indicesCurrent = indicesNext
indicesNext = []int{}
}
}
return res
}
func LongestDupSubstringSmartforceMark3Goroutine(s string, from int, to int) string {
strLen := len(s)
if strLen < 2 {
return ""
}
indicesNext := []int{}
indicesAll := []int{}
for j := 0; j < strLen; j++ {
indicesAll = append(indicesAll, j)
}
res := ""
for i := from; (i < to) && (i < strLen-len(res)); i++ {
indicesCurrent := indicesAll[i+1:]
for ln := 1; ln <= strLen-i; ln++ {
word1 := s[i : i+ln]
for _, index := range indicesCurrent {
if index+ln > strLen {
continue
}
if s[i+ln-1] == s[index+ln-1] {
if len(res) < len(word1) {
res = word1
}
indicesNext = append(indicesNext, index)
}
}
indicesCurrent = indicesNext
indicesNext = []int{}
}
}
return res
}
func LongestDupSubstringGoroutines(s string) string {
threads := 24
results := make([]string, threads)
interval := len(s) / threads
if interval < (800 / threads) {
return LongestDupSubstringSmartforceMark3Goroutine(s, 0, len(s))
}
wg := new(sync.WaitGroup)
wg.Add(threads)
for i := 0; i < threads-1; i++ {
go func(input int) {
results[input] = LongestDupSubstringSmartforceMark3Goroutine(s, input*interval, input*interval+interval)
wg.Done()
}(i)
}
go func(input int) {
results[input] = LongestDupSubstringSmartforceMark3Goroutine(s, input*interval, len(s))
wg.Done()
}(threads - 1)
wg.Wait()
resLen := -1
res := ""
for _, w := range results {
if len(w) > resLen {
resLen = len(w)
res = w
}
}
return res
}
func LongestDupSubstring(s string) string {
return LongestDupSubstringGoroutines(s)
}