Java 递归?串中的组合
我已经处理下面的递归问题有一段时间了,但还没有弄明白。基本上,你有一个由某些单词组成的句子,所有的单词只是挤在一起,而不是隔开。这样做的目的是找到可以用来造句的所有可能的单词组合的数量 比如说,Java 递归?串中的组合,java,string,recursion,dynamic-programming,Java,String,Recursion,Dynamic Programming,我已经处理下面的递归问题有一段时间了,但还没有弄明白。基本上,你有一个由某些单词组成的句子,所有的单词只是挤在一起,而不是隔开。这样做的目的是找到可以用来造句的所有可能的单词组合的数量 比如说, 单词:ook,ookook 句子:Ookook 解决方案:{ook,ook,ook},{ook,ook},{ook,ook} 另一个例子: 单词:ooga,oogam,oogum,mook,ook 句子:oogamookoogumook 解决方案:{ooga,mook,oogum,ook},{oo
- 单词:ook,ookook
- 句子:Ookook
- 解决方案:{ook,ook,ook},{ook,ook},{ook,ook}
- 单词:ooga,oogam,oogum,mook,ook
- 句子:oogamookoogumook
- 解决方案:{ooga,mook,oogum,ook},{oogam,ook,oogum,ook}
public static int WAYS(String word) {
int ways = 1;
for (int i = 0; i < word.length(); i++) {
try{
if(word.substring(i, i - 2).equals("ug")){
if(word.substring(i - 4, i - 2).equals("ug")){
ways++;
}
}
else if(word.substring(i, i - 3).contains("ook")){
System.out.println(word.substring(i-6, i-3));
if(word.substring(i - 6, i - 3).equals("ook")){
ways++;
}
if(word.charAt(i - 4) == 'm'){
if(word.substring(i - 8, i - 4).equals("ooga") || word.substring(i - 8, i - 4).equals("oogu")){
ways++;
}
}
}
else if(word.substring(i, i - 4).contains("mook")){
if(word.substring(i - 8, i - 4).contains("mook")){
ways++;
}
}
if(word.substring(i, i - 2).equals("oog")){
if(word.charAt(i + 2) == 'm'){
if(word.charAt(i + 1) == 'a' || word.charAt(i + 1) == 'u'){
ways++;
}
}
}
} catch(Exception e){
continue;
}
}
return ways;
}
公共静态整数方式(字符串字){
int-ways=1;
for(int i=0;i
但它没有起作用。有人能给我一个使用递归处理这个问题的想法或示例吗?1)正确命名您的方法,“方法”是一个常量名,而不是方法名
2) 提供可运行的代码,特别是在代码很短的情况下
3) 不要对控制流使用异常
4) 您在代码中使用了像“uug”和“ook”这样的神奇值?这看起来简单明了吗?这看起来可维护吗?如果你有一个包含一百万个不同单词的词典,这应该是什么样子
编辑:给出完整的列表有点无聊,所以我留下了一些空白。试着填满这些,希望能有所帮助
public class JammedWords {
public static int ways(String sentence, String[] words) {
if (sentence.isEmpty()) {
// The trivial case: the sentence is empty. Return a single number.
} else {
int c = 0;
for (String w: words) {
if (sentence.startsWith(w)) {
// call method recursively, update counter `c`.
}
}
return c;
}
}
public static void main(String[] args) {
System.out.println(ways("ookookook", new String[]{"ook", "ookook"}));
System.out.println(ways("oogamookoogumook", new String[]{"ooga","oogam","oogum","mook","ook"}));
}
}
提示:
A) 理解空集合、包含空集合的集合、包含包含空集合的集合等之间的区别。包含空集合的集合当然不是空的,它们的大小也不是0
B) 有一个方便的方法String.substring(n),它删除第n个字符之前的所有内容。还有String.length()来获取单词的大小。希望VB.NET代码不会介意,只是为了便于掌握
Private Sub Go()
Dim words As New List(Of String)
words.Add("ooga")
words.Add("oogam")
words.Add("oogum")
words.Add("mook")
words.Add("ook")
Search("oogamookoogumook", words, "", New List(Of String))
End Sub
Private Sub Search(ByVal sentence As String, _
ByVal wordList As List(Of String), _
ByVal actualSentenceBuildingState As String, _
ByVal currentPath As List(Of String))
For Each word As String In wordList
Dim actualSentenceAttemp As String
Dim thisPath As New List(Of String)(currentPath)
thisPath.Add(word)
actualSentenceAttemp = actualSentenceBuildingState + word
If actualSentenceAttemp = sentence Then
Debug.Print("Found: " + String.Join("->", thisPath.ToArray()))
End If
If actualSentenceAttemp.Length < sentence.Length Then 'if we are not too far, we can continue
Search(sentence, wordList, actualSentenceAttemp, thisPath)
End If
Next
End Sub
把它想象成在图形中行走(事实上,它就是这样)。您从零开始(空字符串)。现在,您开始迭代地将单词列表中的单词添加到“当前句子尝试”中。将单词添加到当前调度后,您只能在三种可能的状态下结束:(1)您得到了最后一个句子,(2)当前调度比目标句子短,因此仍然适合添加下一个单词(递归调用),或者(3)您的当前调度比目标序列长(或长度相同但不相等),因此,继续寻找它是没有意义的
您必须记住的是路径--“我是如何到达这里的?”列表(回溯)。您能展示您的递归尝试吗?你真的,真的需要在这个问题上使用递归。像你发布的那样的非递归解决方案是行不通的。@JohnKugelman我的递归尝试非常糟糕,因为我不知道如何用递归正确地解决这个问题。我只需要了解如何处理这类问题的基本知识。谢谢你的想法和提示!
Sentence: oogamookoogumook
Found: ooga->mook->oogum->ook
Found: oogam->ook->oogum->ook
Sentence: ookookook
Found: ook->ook->ook
Found: ook->ookook
Found: ookook->ook