A simple participle program

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. * * Title:**> given string dogandcat and dictionary {"Do", "D", "Gand", "G", "and", "Dog", "cat", "C", "at", "in", "play", "I", "Nplay"},> find Group A combination of possible words into a string. Before using Haskell to write an implementation, but after touching Golang, found can be written in a more concise version, the code is as follows: "" Package Mainimport ("FMT") var word = "Dogandcat" var dicts = []string {"Do", "D", "Gand", "G", "and", "Dog", "cat", "C", "on", "in", "play", "I", "nplay"}var results = [][]string{}var cpycount ER = 0var stepcounter = 0func iscontainsindicts (str string) bool {for _, V: = range Dicts {if v = = str {return True}}retur n False}func Breaker (matched []string, Pos int) {if pos >= len (word) {results = append (results, matched) Return}match: = []byte{}for I: = pos; I < Len (word); i++ {match = append (match, word[i]) Aword: = string (match) Stepcounter + = 1if iscontainsindicts (aword) {cpymatched: = make ([ ]string, Len (matched)) copy (cpymatched, matched) cpymatched = append (cpymatched, aword) Cpycounter + = 1breaker ( cpymatched, I+1)}}}func main () {FMT. PRINTLN (word) fmt. Println (dicts) breaker ([]string{}, 0) fmt. Println () fmt. PRINTLN (results) fmt. Println () fmt. PRINTLN ("Step times:", Stepcounter) fmt. Println ("cpy Times:", Cpycounter)} "The above code also has a little performance problem:-1) If the dictionary is large, iscontainsindicts efficiency is very low;-2) If the string is large, result consumes memory , if there is a better way, welcome advice, Thank you! 174 reads  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.