AC自动机
发布日期:2021-05-14 16:54:59 浏览次数:14 分类:精选文章

本文共 4150 字,大约阅读时间需要 13 分钟。

������ n ������������������ 50 ������������������������������������������������������������ m ���������������������������������������������������������������������������

1. ���������������

������������������������������������������������������������������������������������������������������������������������������������ (BFS) ������������������������������������������

2. ������������������������������

  • ��������� (Prefix Tree) ��� tries������������������������������������������������������������������������������������������������������������������������
  • ������������������ (BFS)������������������������������������������������������������������������

3. ���������������������

  • ���������������������
    • ������������������������������ tr ���������������������tr[p][i] ��������� p ��������������� i ������������������������������������������������������ 0���
    • idx ������������������������������������������
    1. ���������������
      • ������������������������������������������������������������������������ c��������������������������������������������������������������������������������������������������������������������������������� idx ���������������������������������
      • ������������������������������ cnt[p]������������������������������������������
      1. ���������������������������
        • ������������������������������������������������������������
        • ������������ BFS ��������������������������������������������������������������������������������������������������������������� ne ������������������������������������������
        • ���������������������������������������������������������������������������
        1. ���������������������
          • ������������������������������������������������������������������������������

          4. ���������������������

          #include 
          using namespace std;
          const int N = 10010;
          const int S = 55;
          const int M = 1000010;
          char str[M];
          int tr[N * S][26];
          int q[N * S];
          int idx;
          int cnt[N * S];
          int ne[N * S];
          • ������������������������������������������������������������������������������������������������������
          • ���������������str ������������������������������
          • ������������������tr[p][t] ��������� p ��������������� t ���������������
          • ���������q ������������������������������������
          • ������������cnt ������������������������������������
          • ���������������ne ������������������������������������������������

          5. ���������������

          void add() {
          int p = 0;
          for (int i = 0; str[i]; i++) {
          int t = str[i] - 'a';
          if (!tr[p][t]) {
          tr[p][t] = ++idx;
          }
          p = tr[p][t];
          }
          cnt[p]++;
          }
          • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
          • ��������������������������������������������������������� cnt[p]���

          6. ���������������������

          void bfs() {
          int hh = 0, tt = -1;
          for (int i = 0; i < 26; i++) {
          if (tr[0][i]) {
          q[++tt] = tr[0][i];
          }
          }
          while (hh <= tt) {
          int t = q[hh++];
          for (int i = 0; i < 26; i++) {
          int j = ne[t];
          int c = tr[t][i];
          if (!c) continue;
          while (j && !tr[j][i]) j = ne[j];
          if (tr[j][i]) j = tr[j][i];
          ne[c] = j;
          q[++tt] = c;
          }
          }
          }
          • ���������������������������������������������������������������������������
          • ��������������������������������������������������������������������������������������� ne���������������������������������

          7. ������������������

          int main() {
          int t;
          cin >> t;
          while (t--) {
          memset(cnt, 0, sizeof(cnt));
          memset(ne, 0, sizeof(ne));
          memset(tr, 0, sizeof(tr));
          int n;
          cin >> n;
          for (int i = 0; i < n; i++) {
          cin >> str;
          add();
          }
          for (int i = 0; i < M; i++) {
          ... // ������������������
          }
          }
          }
          • ������������������������������������������������ add() ������������������������
          • ������������������������������������������������

          8. ���������������

          ��������������������������������� BFS ������������������������������������������������������������������������������������

    上一篇:ac自动机(求每个单词在单词所组成的论文中出现的次数)
    下一篇:天梯废物我是

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2025年04月24日 07时14分36秒