【问题标题】:prune recursive search paths修剪递归搜索路径
【发布时间】:2012-08-15 17:07:40
【问题描述】:

知识有限,用C++写了2个月

在此函数中,string code 递归递减字符,直到找到基本情况 ""。我想在找到基本情况之前修剪一些路径,对于某些string code,将找不到基本情况的路径。对于修剪,我想将路径中的属性与参数int time 进行比较。这会搜索一个由 'nodeT' 组成的 trie

struct charT {
char letter;
nodeT *next;
};

struct nodeT {
bool isOperation;
bool isCode; 
int time;
Vector<charT> alpha;
};

nodeT *root

usage:
string code = "12345";
int time = convertToEpoch(20120815); //my epoch function
containsCode(code, time)

bool containsCode(string code, int time)
{
    if(root == NULL) return false;
    else return containsCodeHelper(root, code, time);
}


bool containsCodeHelper(nodeT *w, string code, int time)
{
    if(code == "") //base case: all char found
        return w->isCode; 
    else {
        if (w->isOperation && w->time != time) return false; //case 2: time check OK <- at a midpoint in the path
        for(int i = 0; i < w->alpha.size(); i++) { //Loop through the leaf
            if (w->alpha[i].letter == code[0]) //case 3: leaf exists
                return containsCodeHelper(w->alpha[i].next, code.substr(1), time);
        }
    }
    return false; //if no path
}

此函数在添加时间检查修剪之前运行良好,它现在循环,returns false 如果超出时间但随后从字符位置 0 以候选 string code 重新开始。

问题:1) 是一个嵌套的return false 将递归踢回到下一个调用 for 循环,2) 是否应该将时间修剪放在具有逻辑 return falsereturn 'path' 的 for 循环中, 3) 这从根本上是不是一团糟,我需要学习一个 C++ 概念

此外,发布的函数是实际函数的简化版本——我省略了时间修饰符和“跳过”路径。在过去的问题中,我发现这些“插件”会分散问题的注意力。

【问题讨论】:

  • string word? string code?
  • @timrau 修正错字string word -> string code
  • 在您对containsCodeHelper() 的递归调用中,您从未更新参数time。你能解释一下time是什么意思吗?
  • 时代,或多或少;我用containsCode 电话编辑了这个问题
  • 为了检查这个功能,我放置了休息点并在每次休息时跟随汽车 - 是否有这么多路径,我应该创建一个测试用例并让它去看看结果是否正确而不是步骤通过?

标签: c++ visual-studio-2008 recursion trie pruning


【解决方案1】:

经过一些改造后,它现在可以正常工作了——可能一直都是这样;我将属性return w-&gt;isCode改为return true,这似乎是最大的问题——我将调试trie构造函数,看看它是否在每个路径的末尾设置属性。

boolcontainsCodeHelper(nodeT *w, string code, int time) 
{
    if(code == "") //base case: all char found
        return true;
    else {
        if ( w->isOperation && (!((w->begin-wag) <= time && time <= (w->end+wag) ) && time != 9999 ) ) 
            return false; //case 2: time
        else {
            for(int i = 0; i < w->alpha.size(); i++) { //Loop through all of the children of the current node
                if (w->alpha[i].letter == word[0])
                    return containsCodeHelper(w->alpha[i].next, word.substr(1), time, wag);
                else if (word[0] == 'ž') //step over '0' all subnodes
                    if (containsCodeHelper(w->alpha[i].next, word.substr(1), time, wag)) 
                        return true;
            }
        }
    }
    return false; //if char is missing - meaning the exact code is not there - terminates garbage subnode paths
}

我看不出有return false; 结尾和省略它有什么区别。也仍然困惑为什么特殊情况需要if( bool fn()) return true; 而不仅仅是return ( bool fn()); 我在另一个stack overflow thread 的帮助下通过反复试验找到了解决方案

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 2014-03-01
    • 2019-10-25
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多