排列不接受大词

时间:2014-05-26 17:17:44

标签: vb.net

下面的vb.net代码排列给定的单词...我遇到的问题是它不接受像“光合作用”,“日历”等更大的单词,但接受像“book”,“land”这样的较小的单词等等...缺少什么...请帮忙

模块模块1

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

结束模块

1 个答案:

答案 0 :(得分:0)

如果没有实际创建一个项目来运行这段代码,也不知道它是如何“不接受”那么长的话,我的回答是,对于很长的单词有很多排列,而你的程序只需要花费比你长得多的时间我期待着奔跑。所以你可能认为它已经崩溃了。

更新: 问题是递归,它正在炸毁堆栈。您将不得不重写代码以使用迭代而不是递归。一般在这里解释

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

此处的Psuedo代码使用迭代而不是递归

Generate list of all possible permutations of a string