結果

問題 No.430 文字列検索
ユーザー 👑 emthrmemthrm
提出日時 2019-08-31 04:02:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 3,288 bytes
コンパイル時間 1,311 ms
コンパイル使用メモリ 99,556 KB
実行使用メモリ 7,676 KB
最終ジャッジ日時 2024-05-02 04:34:38
合計ジャッジ時間 2,309 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 19 ms
7,676 KB
testcase_02 AC 5 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 10 ms
5,468 KB
testcase_12 AC 11 ms
5,760 KB
testcase_13 AC 11 ms
5,756 KB
testcase_14 AC 9 ms
5,472 KB
testcase_15 AC 7 ms
5,464 KB
testcase_16 AC 7 ms
5,468 KB
testcase_17 AC 8 ms
5,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <queue>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
/*-------------------------------------------------*/
template <size_t char_sz = 26>
struct Trie {
  struct Node {
    char c;
    int nx[char_sz];
    vector<int> tails;
    Node(char c) : c(c) { memset(nx, -1, sizeof(nx)); }
  };

  vector<Node> nodes;

  Trie() { nodes.emplace_back('$'); }

  void add(const string &str, int id = -1, int pos = 0) {
    for (char c : str) {
      int now = convert(c);
      if (nodes[pos].nx[now] == -1) {
        int nx_pos = nodes.size();
        nodes[pos].nx[now] = nx_pos;
        nodes.emplace_back(c);
        pos = nx_pos;
      } else {
        pos = nodes[pos].nx[now];
      }
    }
    nodes[pos].tails.emplace_back(id);
  }

  int find(const string &t, int pos = 0) {
    for (char c : t) {
      int now = convert(c);
      if (nodes[pos].nx[now] == -1) return -1;
      pos = nodes[pos].nx[now];
    }
    return pos;
  }

  int convert(char c) { return c - 'A'; }
};

template <size_t char_sz = 26>
struct AhoCorasick : Trie<char_sz + 1> {
  vector<int> cnt;

  void build() {
    auto &vertices = this->nodes;
    int n = vertices.size();
    cnt.resize(n);
    REP(i, n) {
      sort(ALL(vertices[i].tails));
      cnt[i] = vertices[i].tails.size();
    }
    queue<int> que;
    REP(i, char_sz) {
      if (vertices[0].nx[i] == -1) {
        vertices[0].nx[i] = 0;
      } else {
        vertices[vertices[0].nx[i]].nx[char_sz] = 0;
        que.emplace(vertices[0].nx[i]);
      }
    }
    while (!que.empty()) {
      auto node = vertices[que.front()];
      cnt[que.front()] += cnt[node.nx[char_sz]];
      que.pop();
      REP(i, char_sz) if (node.nx[i] != -1) {
        int on_failure = node.nx[char_sz];
        while (vertices[on_failure].nx[i] == -1) on_failure = vertices[on_failure].nx[char_sz];
        vertices[node.nx[i]].nx[char_sz] = vertices[on_failure].nx[i];
        auto &ver = vertices[node.nx[i]].tails;
        vector<int> tmp;
        set_union(ALL(ver), ALL(vertices[vertices[on_failure].nx[i]].tails), back_inserter(tmp));
        ver.resize(tmp.size());
        copy(ALL(tmp), ver.begin());
        que.emplace(node.nx[i]);
      }
    }
  }

  int move(char c, int pos) {
    int now = this->convert(c);
    while (this->nodes[pos].nx[now] == -1) pos = this->nodes[pos].nx[char_sz];
    return pos = this->nodes[pos].nx[now];
  }

  // map<int, int>
  int match(const string &t, int pos = 0) {
    int total = 0;
    // map<int, int> mp;
    for (char c : t) {
      pos = move(c, pos);
      total += this->nodes[pos].tails.size();
      // for (int e : this->nodes[pos].tails) ++mp[e];
    }
    return total;
    // return mp;
  }
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  string s; cin >> s;
  AhoCorasick<> aho;
  int m; cin >> m;
  REP(i, m) {
    string p; cin >> p;
    aho.add(p, i);
  }
  aho.build();
  cout << aho.match(s) << '\n';
  return 0;
}
0