在Aho-Corasick自动机中寻找循环

时间:2013-03-03 18:16:39

标签: c++ algorithm

我正面临一个应该使用Aho-Corasick自动机解决的问题。我给了一组单词(用'0'或'1'组成) - 模式,我必须决定是否有可能创建无限的文本,这些文本不包含任何给定的模式。我认为,解决方案是创建Aho-Corasick自动机并搜索没有匹配状态的循环,但我无法提出一个好的方法来做到这一点。我想过使用DFS搜索状态图,但是我不确定它是否会起作用并且我有一个实现问题 - 让我们假设,我们处于一个状态,它有一个'1'边缘 - 但状态指向edge被标记为匹配 - 所以我们不能使用那个边缘,我们可以尝试失败链接(当前状态没有'0'边缘) - 但我们还必须记住,我们不能从状态指向'1'边缘通过当前的失败链接。

有人能纠正我并告诉我该怎么做吗?我用C ++编写了Aho-Corasick,我确信它有效 - 我也理解整个算法。

以下是基本代码:

class AhoCorasick
{

static const int ALPHABET_SIZE = 2;

struct State
{
  State* edge[ALPHABET_SIZE];
  State* fail;
  State* longestMatchingSuffix;
  //Vector used to remember which pattern matches in this state.
  vector< int > matching;
  short color;

  State()
  {
    for(int i = 0; i < ALPHABET_SIZE; ++i)
      edge[i] = 0;
    color = 0;
  }

  ~State()
  {
    for(int i = 0; i < ALPHABET_SIZE; ++i)
    {
      delete edge[i];
    }
  }
};

private:
  State root;
  vector< int > lenOfPattern;
  bool isFailComputed;

  //Helper function used to traverse state graph.
  State* move(State* curr, char letter)
  {
    while(curr != &root && curr->edge[letter] == 0)
    {
      curr = curr->fail;
    }
    if(curr->edge[letter] != 0)
      curr = curr->edge[letter];
    return curr;
  }

  //Function which computes fail links and longestMatchingSuffix. 
  void computeFailLink()
  {
    queue< State* > Q;
    root.fail = root.longestMatchingSuffix = 0;
    for(int i = 0; i < ALPHABET_SIZE; ++i)
    {
      if(root.edge[i] != 0)
      {
        Q.push(root.edge[i]);
        root.edge[i]->fail = &root;
      }
    }
    while(!Q.empty())
    {
      State* curr = Q.front();
      Q.pop();
      if(!curr->fail->matching.empty())
      {
        curr->longestMatchingSuffix = curr->fail;
      }
      else
      {
        curr->longestMatchingSuffix = curr->fail->longestMatchingSuffix;
      }
      for(int i = 0; i < ALPHABET_SIZE; ++i)
      {
        if(curr->edge[i] != 0)
        {
          Q.push(curr->edge[i]);
          State* state = curr->fail;
          state = move(state, i);
          curr->edge[i]->fail = state;
        }
      }
    }
    isFailComputed = true;
  }

public:
  AhoCorasick()
  {
    isFailComputed = false;
  }

  //Add pattern to automaton.
  //pattern - pointer to pattern, which will be added
  //fun - function which will be used to transform character to 0-based index.
  void addPattern(const char* const pattern, int (*fun) (const char *))
  {
    isFailComputed = false;
    int len = strlen(pattern);
    State* curr = &root;
    const char* pat = pattern;
    for(; *pat; ++pat)
    {
      char tmpPat = fun(pat);
      if(curr->edge[tmpPat] == 0)
      {
        curr = curr->edge[tmpPat] = new State;
      }
      else
      {
        curr = curr->edge[tmpPat];
      }
    }
    lenOfPattern.push_back(len);
    curr->matching.push_back(lenOfPattern.size() - 1);
  }
};

int alphabet01(const char * c)
{
  return *c - '0';
}

1 个答案:

答案 0 :(得分:0)

我没有查看您的代码,但我知道非常简单有效的实现。

首先,让我们将词典后缀链接添加到树中(您可以在维基百科中找到它们的描述)。然后你必须浏览所有的树,并以某种方式标记匹配的节点和节点,将Dict后缀链接作为坏节点。这些操作的解释很明显:您不需要所有匹配的节点,也不需要具有匹配后缀的节点。

现在你有一个没有任何匹配节点的Aho-Corasick树。如果您只在结果树上运行DFS算法,您将得到您想要的。