HDU2222 LibreOJ10057 Keywords Search

題目連結 HDU
題目連結 LibreOJ

  • 題意:給定 $N$ 個 keyword 和一個匹配字串,問多少個 keyword 出現在匹配字串裡。
  • 題解:AC 自動機裸題。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    #pragma GCC optimize(2)
    #include <bits/stdc++.h>
    using namespace std;
    typedef pair<int, int> PII;
    typedef long long LL;
    const int INF = 1e9;
    const int MXN = 3e5 + 5;
    const int MXS = 1e7 + 5;
    const int MXW = 26;
    const LL MOD = 10009;
    const LL seed = 31;
    #define MP make_pair
    #define PB push_back
    #define F first
    #define S second
    #define FOR(i, L, R) for (int i = L; i != (int)R; ++i)
    #define FORD(i, L, R) for (int i = L; i != (int)R; --i)
    #define IOS \
    cin.tie(NULL); \
    cout.tie(NULL); \
    ios_base::sync_with_stdio(false);

    struct Node
    {
    char ch;
    int v;
    Node *next[MXW];
    Node *fail;
    Node(): v(0), fail(0) { memset(next,0,sizeof(next)); }
    };

    void insert(Node *root, char *s)
    {
    int sz = strlen(s);
    FOR(i, 0, sz)
    {
    int v = s[i] - 'a';
    if (root->next[v] == NULL)
    {
    root->next[v] = new Node();
    }
    root = root->next[v];
    root->ch = s[i];
    }
    ++root->v;
    }

    queue<Node *> q;
    void bulidAC(Node *root)
    {
    Node *k, *tmp;
    FOR(i, 0, MXW)
    {
    if (root->next[i] != NULL)
    {
    root->next[i]->fail = root;
    q.push(root->next[i]);
    }
    }
    while (!q.empty())
    {
    k = q.front();
    q.pop();
    FOR(i, 0, MXW) if (k->next[i] != NULL)
    {
    tmp = k->fail;
    while (tmp != NULL)
    {
    if (tmp->next[i] != NULL)
    {
    k->next[i]->fail = tmp->next[i];
    break;
    }
    tmp = tmp->fail;
    }
    if (tmp == NULL)
    {
    k->next[i]->fail = root;
    }
    q.push(k->next[i]);
    }
    }
    }

    int ans;
    void acAutomation(Node *root, char *s)
    {
    Node *p = root;
    int sz = strlen(s);
    FOR(i, 0, sz)
    {
    int v = s[i] - 'a';
    while (p->next[v] == NULL && p != root)
    {
    p = p->fail;
    }
    p = p->next[v];
    if (p == NULL)
    {
    p = root;
    }
    Node *k = p;
    while (k != root)
    {
    if (k->v >= 0)
    {
    ans += k->v;
    k->v = -1;
    }
    else
    {
    break;
    }
    k = k->fail;
    }
    }
    }

    char s[MXS];
    int main()
    {
    int t;
    scanf("%d", &t);
    while (t--)
    {
    int n;
    Node *root = new Node();
    scanf("%d", &n);
    while (n--)
    {
    scanf("%s", s);
    insert(root, s);
    }
    bulidAC(root);
    scanf("%s", s);
    ans = 0;
    acAutomation(root, s);
    printf("%d\n", ans);
    }
    }

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Recommended Posts