当前位置:网站首页>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 :
边栏推荐
- Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)
- Easygbs adds websocket message push, which can quickly locate video playback faults
- Summary of easy-to-use MySQL interview questions (Part 1)
- 5. concept of ruler method
- PNAs: power spectrum shows obvious bold resting state time process in white matter
- 2021-11-11
- OVS port traffic statistics practice
- Analysis of resolv Conf common parameters
- 862. triple sorting
- How to make a borrowing card
猜你喜欢

Performance test -- 14 detailed explanation of performance test report and precautions

Docker installs mysql5.7 and mounts the configuration file

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

Interviewer: why does TCP shake hands three times and break up four times? Most people can't answer!

Log a log4j2 vulnerability handling

Xgboost Guide

Information theory and coding

For Xiaobai who just learned to crawl, you can understand it after reading it

Why is BeanUtils not recommended?
随机推荐
Small knowledge points of asset
Ansible practice of Nepal graph
EDI project cases of customers in medical device industry
This monitoring tool is enough for the operation and maintenance of small and medium-sized enterprises - wgcloud
Add other view components to the audio and video components of the applet
Ugui empty button implementation
Learning notes of recommendation system (1) - Collaborative Filtering - Theory
Reptile lesson 1
Delta oscillation in EEG
January 31, 2022: Maze III. There is a ball in the maze of open spaces and walls. ball
[target tracking] open source | polytrack: use boundary polygons to quickly track and segment multiple targets, instead of bounding box and mask tracking
862. triple sorting
February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
Nebula operator cloud practice
Special exercise split line-----------------------------
Easygbs adds websocket message push, which can quickly locate video playback faults
Third order magic cube formula
5g access network and base station evolution
Buuctf misc-[actf freshman competition 2020]outline
Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed