【问题标题】:write trie parsing recursive function with node step over编写带有节点跨步的trie解析递归函数
【发布时间】:2012-08-08 21:05:05
【问题描述】:

目的:该函数通过匹配输入字符串路径的字符串trie进行解析。解析完字符串中的所有字符后,返回true。如果仍有有效路径,我想跳过一个字符并返回。

应用程序:字符串是高速公路项目的位置层次结构。因此,项目 5 具有路线 C,其偏移量为 N,工作区为 3; 5CN3。但是,有时我想为涵盖所有位置的项目任务的所有子位置定义一个字符串。因此,“0”是所有位置;对于像坡度泥土这样的半天操作,没有工作区 - 代表这项任务的所有工作区都是北线 C 中的所有工作区; 5CN0.如果一个操作涵盖整个项目,则相同; 5000。

方法:我可以使用通配符“?”功能,但我想保留此特定步骤以抽象位置。也许 '?'是正确的方法,但似乎失去了一些控制。此外,这可以在没有 for 循环的情况下编写并使用位置索引参数;也许这就是问题所在 - 可能是回溯。

代码:nodeT是trie节点,word是输入字符串,这个函数是一个bool,如果字符串路径存在则返回1/0。

bool Lexicon::containsWordHelper(nodeT *w, string word)) //check if prefix can be combined
{
    if(word == "") { //base case: all char found
        return true;
    } 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 containsWordHelper(w->alpha[i].next, word.substr(1));
            else if (word[0] == '0') //if '0' then step over and continue searching for valid path
                containsWordHelper(w->alpha[i].next, word.substr(1)); //removed return here to allow looping through all the possible paths
        } //I think it is continuing through after the loop and triggering return false
    }
    return false; //if char is missing - meaning the exact code is not there
}

问题是当使用“0”通配符时返回 false。这里出了什么问题?我的知识有限。

我在这个问题上研究了一段时间,并使用了“howboutthis howboutthat”方法,发现将return放在step over语句的末尾是可行的。

bool Lexicon::containsWordHelper(nodeT *w, string word, int &time, int &wag, string compare) //check if prefix can be combined
{
    if(word == "") { //base case: all letters found
        if ((w->begin-wag) <= time && time <= (w->end+wag)) 
            return w->isWord; // case 2: timecard check for high/low date range
        else if (time == ConvertDateToEpoch(9999, 01, 01)) return w->isWord; //this is for single code lookup w/o date
    } 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 containsWordHelper(w->alpha[i].next, word.substr(1), time, wag, compare);
            else if (word[0] == 'ž')
                if (containsWordHelper(w->alpha[i].next, word.substr(1), time, wag, compare)) return true;
        }
    }
    return false; //if char is missing - meaning the exact code is not there
}

如果我只有一个以 true 结尾的路径来返回,那么我应该在递归完成后放置 return,然后只有在 true 时有条件地返回,这似乎是合乎逻辑的。回想起来它很有效,而且似乎合乎逻辑,但我对此的信心充其量只是粗略的。

我仍然有同样的问题。出了什么问题?

【问题讨论】:

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


    【解决方案1】:

    你可以测试后一个containsWordHelper调用的结果,如果结果为真则返回真,否则继续迭代。

    【讨论】:

    • 删除了returnsif(word=="") return true 除外,它遵循一条路径,返回 true,但随后继续 for 循环,最终/以某种方式测试为 false。
    • 删除 returns 基本情况除外 return 如果这是您要说的 - 不起作用。
    【解决方案2】:

    已解决:在包含递归调用的 if 语句后放置一个 return

    bool Lexicon::containsWordHelper(nodeT *w, string word) 
        {
            if(word == "") return w->isWord;
            else {
                for(int i = 0; i < w->alpha.size(); i++) {
                    if (w->alpha[i].letter == word[0])
                        return containsWordHelper(w->alpha[i].next, word.substr(1));
                    else if (word[0] == 'ž')
                        if (containsWordHelper(w->alpha[i].next, word.substr(1))) return true;
                }
            }
            return false;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-05-15
      • 2018-08-16
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2013-04-21
      相关资源
      最近更新 更多