下面的vb.net代码改变了我遇到的一个给定的word...the问题,即它不接受“光合作用”、“日历”等较大的单词,而接受“书”、“土地”等较小的单词。...what是missing...Pls帮助。
模块Module1
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结束模块
发布于 2014-05-27 01:28:49
我没有真正创建一个项目来运行这段代码,也不知道它是如何“不接受”长词的,我的答案是,长词有很多排列,你的程序运行的时间比你预期的要长得多。所以你可能会认为它已经崩溃了。
更新:问题是递归,它正在炸毁堆栈。你必须重写你的代码来使用迭代而不是递归。一般在这里解释
http://www.refactoring.com/catalog/replaceRecursionWithIteration.html
这里的Psuedo代码使用迭代而不是递归
https://stackoverflow.com/questions/23874969
复制相似问题