Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带剪枝搜索的递归回溯_C++_Visual Studio 2008_Recursion_Backtracking - Fatal编程技术网

C++ 带剪枝搜索的递归回溯

C++ 带剪枝搜索的递归回溯,c++,visual-studio-2008,recursion,backtracking,C++,Visual Studio 2008,Recursion,Backtracking,我有一个递归函数,它解析一个trie字符串数据库,替换为每个节点中的所有字符。在递归调用中,增加编辑计数,然后测试新字符串1)如果所有节点都已解析,2)如果该字符串等于已批准字符串列表中的字符串。因此,结果应该是测试字符串和数据库中所有可能字符串之间的编辑距离 void suggestCorrections(nodeT *w, Set<CorrectionT> &suggest, int index, string test, string final, double edi

我有一个递归函数,它解析一个trie字符串数据库,替换为每个节点中的所有字符。在递归调用中,增加编辑计数,然后测试新字符串1)如果所有节点都已解析,2)如果该字符串等于已批准字符串列表中的字符串。因此,结果应该是测试字符串和数据库中所有可能字符串之间的编辑距离

void suggestCorrections(nodeT *w, Set<CorrectionT> &suggest, int index, string test, string final, double edits)
{   
    if (word == "") {
        if (containsCode(final)) //compare with list of approved strings
            updateSet(w, suggest, final, edits);

    } else  { //continue to search path
        if( w->alpha.size() > index) {
            if (test[0] == w->alpha[index].char)
                suggestCorrections(w->alpha[index].next, suggest, 0, test.substr(1), final+test[0], edits); //follow matching char 
            suggestCorrections(w, suggest, ++index, test, final, edits);//check next path
            suggestCorrections(w->alpha[index].next, suggest, 0, test.substr(1), final+w->alpha[index].char, ++edits);//follow path
        }
    }
}

struct CorrectionT {
    double editDistance; //stackItems
    string suggestedWord; //stackItems
    string suggestDescription;
    string gates;
};
void suggestCorrections(nodeT*w、Set&suggest、int索引、字符串测试、字符串最终版、双重编辑)
{   
如果(单词==“”){
if(containsCode(final))//与批准的字符串列表进行比较
更新集(w、建议、最终、编辑);
}否则{//继续搜索路径
如果(w->alpha.size()>索引){
如果(测试[0]==w->alpha[index].char)
suggestCorrections(w->alpha[index]。下一步,suggest,0,test。substr(1),final+test[0],edits);//跟随匹配字符
SuggestCorrection(w,suggest,++索引,测试,最终,编辑);//检查下一个路径
suggestCorrections(w->alpha[index]。接下来,suggest,0,test.substr(1),final+w->alpha[index]。char,++edits);//跟随路径
}
}
}
结构校正{
double editDistance;//堆栈项
string suggestedWord;//stackItems
字符串提示描述;
串门;
};
问题是当w->alpha.size()等于index时-路径正确结束,但最后一条路径
suggestcorrection(w->alpha[index]。接下来,suggest,0,test.substr(1),final+w->alpha[index].char,++edits)alpha结尾时,输入代码>。为什么会发生这种情况?它是如何修复的


我认为当路径的末端被满足时,它是通过函数回溯的。我不想让它倒退。我检查了调用堆栈,设置中断,但这似乎是一个概念问题,而不是一个bug。我还阅读了教科书中关于递归和Wikipedia页面的一章——我理解回溯的概念,但不理解在这种特定情况下的实现(或期望的缺乏)。我使用回溯来构建trie数据库,它在那里工作,但这与这里的情况大不相同,我无法理解。

在suggestCorrections的第二个到最后一个调用中,您正在传递++索引。这就是增加index的值,然后在最后一次调用suggestCorrections时传入该值。我并没有真正尝试理解您的代码,但看起来您可能只是想在倒数第二个调用中传递index+1,而不是++index


一般来说,在函数调用参数中隐藏增量操作不是好的编码实践(至少在我看来)。这使得读取和调试代码变得很困难。

我在早期版本中使用了
index+1
,而
++index
看起来很酷,所以我更改了它。你能解释一下为什么在编译器中这在功能上是不同的吗?从
++index
index+1
的变化没有太大作用++index修改了index的值,所以以后在同一函数中引用index时,它将使用递增的值。因此,如果在if语句中使用index==size,那么在最后一次调用suggestcorrections时,它将等于size+1。为了更明确一点,
++index
基本上等同于说
index=index+1
。我理解,改为
index+1
。如果我理解正确,您的观点是,如果此函数有一个调用,并且只在使用
索引时使用它,那么
++index
就可以了。但是,函数中有几种
index
用法,因此必须使用
index+1
。我在每个递归调用中添加了return-->
return suggestcorrection(w->alpha[index]。下一步而且,现在它没有回溯-我真的很想了解我在这里做什么。黑客解决方案,这个怎么样,那个看起来有点粗略。