1

我在这里得到了 aho-corasick 算法的代码:http ://www.komodia.com/aho-corasick 。

我按照指南所说的那样使用它,添加了线条并构建了树。

然而,我确实将它从使用 std wstring 更改为 std string 但这无关紧要。我刚刚更改了typedef。

当我使用它并搜索某些东西时,如果没有找到结果就没有问题。找到结果后,我得到一个标准超出范围异常。

它在这里崩溃:

        if (aIterator==pNode->aMap.end())
            //No, check if we have failure node
            if (!pNode->pFailureNode)
            {
                //No failure node, start at root again
                pNode=&m_aRoot;

                //Reset search string
                sMatchedString="";

                //Did we do a switch?
                if (bSwitch)
                    //We need to do this over
                    --iCount;

                //Exit this loop
                break;
            }
            else
            {
                //What is the depth difference?
                unsigned short usDepth;
                usDepth=pNode->usDepth-pNode->pFailureNode->usDepth-1;

                //This is how many chars to remove
                sMatchedString=sMatchedString.substr(usDepth,sMatchedString.length()-usDepth); //CRASHES HERE!!

                //Go to the failure node
                pNode=pNode->pFailureNode;

                //Set to switch
                bSwitch=true;
            }
        else
        {
            //Add the char
            sMatchedString+=rString[iCount];

            //Save the new node
            pNode=aIterator->second;

            //Exit the loop
            break;
        }
    }

它在这里崩溃:

sMatchedString=sMatchedString.substr(usDepth,sMatchedString.length()-usDepth); 

以下是变量:

在此处输入图像描述

我正在使用它在游戏中实施审查。

什么可能导致它崩溃?

我有一些字符串添加了两次,这会导致问题吗?

谢谢

4

1 回答 1

1

一个可能的问题是sMatchedString"u"而 usDepth 为 3。这导致来自第三个字符(在一个字符串中)的子字符串,长度为 -2。

于 2012-06-05T06:33:19.417 回答