首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不接受大词的排列

不接受大词的排列
EN

Stack Overflow用户
提问于 2014-05-27 01:17:44
回答 1查看 62关注 0票数 0

下面的vb.net代码改变了我遇到的一个给定的word...the问题,即它不接受“光合作用”、“日历”等较大的单词,而接受“书”、“土地”等较小的单词。...what是missing...Pls帮助。

模块Module1

代码语言:javascript
复制
Sub Main()
    Dim strInputString As String = String.Empty
    Dim lstPermutations As List(Of String)
    'Loop until exit character is read
    While strInputString <> "x"
        Console.Write("Please enter a string or x to exit: ")
        strInputString = Console.ReadLine()
        If strInputString = "x" Then
            Continue While
        End If
        'Create a new list and append all possible permutations to it.
        lstPermutations = New List(Of String)
        Append(strInputString, lstPermutations)

        'Sort and display list+stats
        lstPermutations.Sort()
        For Each strPermutation As String In lstPermutations
            Console.WriteLine("Permutation: " + strPermutation)
        Next
        Console.WriteLine("Total: " + lstPermutations.Count.ToString)
        Console.WriteLine("")
    End While
End Sub

Public Sub Append(ByVal pString As String, ByRef pList As List(Of String))
    Dim strInsertValue As String
    Dim strBase As String
    Dim strComposed As String
    'Add the base string to the list if it doesn't exist

    If pList.Contains(pString) = False Then
        pList.Add(pString)
    End If
    'Iterate through every possible set of characters
    For intLoop As Integer = 1 To pString.Length - 1
        'we need to slide and call an interative function.
        For intInnerLoop As Integer = 0 To pString.Length - intLoop
            'Get a base insert value, example (a,ab,abc)
            strInsertValue = pString.Substring(intInnerLoop, intLoop)
            'Remove the base insert value from the string eg (bcd,cd,d)
            strBase = pString.Remove(intInnerLoop, intLoop)
            'insert the value from the string into spot and check
            For intCharLoop As Integer = 0 To strBase.Length - 1
                strComposed = strBase.Insert(intCharLoop, strInsertValue)
                If pList.Contains(strComposed) = False Then
                    pList.Add(strComposed)
                    'Call the same function to review any sub-permutations.
                    Append(strComposed, pList)
                End If
            Next
        Next
    Next
End Sub

结束模块

EN

回答 1

Stack Overflow用户

发布于 2014-05-27 01:28:49

我没有真正创建一个项目来运行这段代码,也不知道它是如何“不接受”长词的,我的答案是,长词有很多排列,你的程序运行的时间比你预期的要长得多。所以你可能会认为它已经崩溃了。

更新:问题是递归,它正在炸毁堆栈。你必须重写你的代码来使用迭代而不是递归。一般在这里解释

http://www.refactoring.com/catalog/replaceRecursionWithIteration.html

这里的Psuedo代码使用迭代而不是递归

Generate list of all possible permutations of a string

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23874969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档