当前位置:网站首页>January 29, 2022: connectives. Give you an array of strings without repeated words
January 29, 2022: connectives. Give you an array of strings without repeated words
2022-06-23 02:45:00 【Fuda scaffold constructor's daily question】
2022-01-29: Conjunctions .
To give you one Without repetition String array of words words , Please find out and return to words All in Conjunctions .
Conjunctions Defined as : A string consisting entirely of at least two shorter words in a given array .
Input :words = "cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"
Output :"catsdogcats","dogcatsdog","ratcatdogcat"
explain :"catsdogcats" from "cats", "dog" and "cats" form ;
"dogcatsdog" from "dog", "cats" and "dog" form ;
"ratcatdogcat" from "rat", "cat", "dog" and "cat" form .
Power button 472.
answer 2022-01-29:
Prefix tree , Try the model from left to right .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"sort"
)
func main() {
words := []string{"cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"}
ret := findAllConcatenatedWordsInADict2(words)
fmt.Println(ret)
}
type TrieNode struct {
end bool
nexts []*TrieNode
}
func NewTrieNode() *TrieNode {
ans := &TrieNode{}
ans.end = false
ans.nexts = make([]*TrieNode, 26)
return ans
}
func insert(root *TrieNode, s []byte) {
path0 := 0
for _, c := range s {
path0 = int(c - 'a')
if root.nexts[path0] == nil {
root.nexts[path0] = NewTrieNode()
}
root = root.nexts[path0]
}
root.end = true
}
// Prepare the dynamic planning table in advance
var dp = make([]int, 1000)
// Method 2 : Prefix tree optimization + Dynamic planning optimization
func findAllConcatenatedWordsInADict2(words []string) []string {
ans := make([]string, 0)
if len(words) < 3 {
return ans
}
sort.Slice(words, func(i, j int) bool {
str1 := words[i]
str2 := words[j]
return len(str1) < len(str2)
})
root := NewTrieNode()
for _, str := range words {
s := []byte(str)
for i := 0; i < len(s)+1; i++ {
dp[i] = 0
}
if len(s) > 0 && split2(s, root, 0, dp) {
ans = append(ans, str)
} else {
insert(root, s)
}
}
return ans
}
func split2(s []byte, r *TrieNode, i int, dp []int) bool {
if dp[i] != 0 {
return dp[i] == 1
}
ans := false
if i == len(s) {
ans = true
} else {
c := r
for end := i; end < len(s); end++ {
path0 := s[end] - 'a'
if c.nexts[path0] == nil {
break
}
c = c.nexts[path0]
if c.end && split2(s, r, end+1, dp) {
ans = true
break
}
}
}
dp[i] = twoSelectOne(ans, 1, -1)
return ans
}
func twoSelectOne(c bool, a, b int) int {
if c {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- How to use fortress on mobile devices
- February 2, 2022: the closest binary search tree value II. Given a non empty two
- 862. triple sorting
- [target tracking] open source | polytrack: use boundary polygons to quickly track and segment multiple targets, instead of bounding box and mask tracking
- 5. concept of ruler method
- Salesforce fileUpload (I) how to configure the file upload function
- PNAs: power spectrum shows obvious bold resting state time process in white matter
- Ping your domain name to 127.0.0.1. The solution to DNS hijacking
- Weekly Postgres world news 2022w04
- How to make keyword targeted layout based on search sources?
猜你喜欢

Custom shapes for ugui skill learning

Performance test -- Jenkins environment construction for 15jmeter performance test
What is sitelock? What is the function?

Why is BeanUtils not recommended?

Unity official case nightmare shooter development summary < I > realization of the role's attack function

Vulnhub DC-5

5g access network and base station evolution

5. concept of ruler method

Docker installs mysql5.7 and mounts the configuration file

My good brother gave me a difficult problem: retry mechanism
随机推荐
Docker builds redis3 master-slave cluster and expands the capacity
How to customize a finished label template
Performance testing -- Interpretation and practice of 16 enterprise level project framework
Pnas: amygdala individual specific functional connectivity: Fundamentals of precision psychiatry
Troubleshooting and solution of 4K video cannot be played on easydss live video on demand platform
The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
Stop automatically after MySQL starts (unable to start)
EDI project cases of customers in medical device industry
OVS port traffic statistics practice
Operate attribute chestnut through reflection
Digital integrated circuit design process
Deep learning environment configuration (III) pytorch GPU under Anaconda
C language series - Section 4 - arrays
Detailed explanation of online reputation management
Applet control version update best practices
Why is BeanUtils not recommended?
Canvas draw the clock
Deep learning environment configuration (I) installation of CUDA and cudnn
2021-11-11
2022-01-30: minimum good base. For a given integer n, if K (k) of n