首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用aho-corasick算法崩溃?

使用aho-corasick算法崩溃?
EN

Stack Overflow用户
提问于 2012-06-05 14:27:40
回答 1查看 309关注 0票数 1

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

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

但是,我确实将其从使用std wstring改为使用std string,但这并不重要。我刚刚更改了typedef。

当我使用它并搜索一些东西时,如果没有找到结果,就没有问题。当找到结果时,我得到一个std out of range异常。

它在这里崩溃:

代码语言:javascript
复制
        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;
        }
    }

它在这里崩溃:

代码语言:javascript
复制
sMatchedString=sMatchedString.substr(usDepth,sMatchedString.length()-usDepth); 

以下是变量:

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

什么会导致它崩溃?

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-05 14:33:19

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10892646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档