我在这里获得了aho-corasick算法的代码:http://www.komodia.com/aho-corasick。
我按照指南所说的那样使用了它,添加了行并构建了树。
但是,我确实将其从使用std wstring改为使用std string,但这并不重要。我刚刚更改了typedef。
当我使用它并搜索一些东西时,如果没有找到结果,就没有问题。当找到结果时,我得到一个std out of range异常。
它在这里崩溃:
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); 以下是变量:

我正在使用它在游戏中实现审查。
什么会导致它崩溃?
我有一些字符串添加了两次,这会造成问题吗?
谢谢
发布于 2012-06-05 14:33:19
一个可能的问题是,sMatchedString是"u",而usDepth是3。这会导致从第三个字符开始的子字符串(在一个字符串中)的长度为-2。
https://stackoverflow.com/questions/10892646
复制相似问题