結果

問題 No.430 文字列検索
ユーザー ferinferin
提出日時 2018-10-05 13:38:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 3,595 bytes
コンパイル時間 1,380 ms
コンパイル使用メモリ 175,328 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2024-04-20 16:43:09
合計ジャッジ時間 2,151 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 14 ms
10,240 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 11 ms
7,724 KB
testcase_12 AC 11 ms
8,448 KB
testcase_13 AC 11 ms
8,448 KB
testcase_14 AC 8 ms
7,204 KB
testcase_15 AC 6 ms
6,940 KB
testcase_16 AC 6 ms
6,940 KB
testcase_17 AC 6 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using PII = pair<int, int>;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll INF = (1LL<<60);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

// 文字の種類数をtemplate引数で渡す
template <int types = 26>
struct AhoCorasick {
  // trie木のnode
  struct node {
    node *fail;
    V<node*> next;
    V<int> matched;
    node() : fail(nullptr), next(types, nullptr) {}
  };

  // 辞書のサイズ
  int sz;
  // trie木の根
  node *root;
  // vectorを結合
  V<int> unite(const V<int> &a, const V<int> &b) {
    V<int> ret;
    set_union(ALL(a), ALL(b), back_inserter(ret));
    return ret;
  }
  // 文字と数字の対応付けをする関数
  function<int(char)> trans;
  // 初期化
  AhoCorasick() {}
  AhoCorasick(V<string> pattern, function<int(char)> f = [](char c){return c-'a';}) {
    trans = f;
    build(pattern);
  }
  // 文字列集合patternからtrie木っぽいオートマトンを作成
  void build(V<string> pattern) {
    sz = pattern.size(), root = new node;
    node *now;
    root->fail = root;
    REP(i, sz) {
      now = root;
      for(const auto &c: pattern[i]) {
        if(now->next[trans(c)] == nullptr) {
          now->next[trans(c)] = new node;
        }
        now = now->next[trans(c)];
      }
      now->matched.PB(i);
    }

    queue<node*> que;
    REP(i, types) {
      if(root->next[i] == nullptr) {
        root->next[i] = root;
      } else {
        root->next[i]->fail = root;
        que.push(root->next[i]);
      }
    }
    while(que.size()) {
      now = que.front(); que.pop();
      REP(i, types) {
        if(now->next[i] != nullptr) {
          node *nxt = now->fail;
          while(!nxt->next[i]) nxt = nxt->fail;
          now->next[i]->fail = nxt->next[i];
          now->next[i]->matched = unite(now->next[i]->matched, nxt->next[i]->matched);
          que.push(now->next[i]);
        }
      }
    }
  }
  // 一文字ずつ照合していく
  node* next(node* p, const char c) {
    while(p->next[trans(c)] == nullptr) p = p->fail;
    return p->next[trans(c)];
  }
  // 文字列s中に辞書と一致する部分列がどれだけあるか
  V<int> match(const string s) {
    V<int> res(sz);
    node *now = root;
    for(auto c : s) {
      now = next(now, c);
      for(auto i : now->matched) res[i]++;
    }
    return res;
  }
};

signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  int n;
  cin >> n;
  V<string> v(n);
  REP(i, n) cin >> v[i];

  AhoCorasick<26> aho(v, [&](char c){ return c-'A'; });
  auto ret = aho.match(s);

  int ans = 0;
  for(int i: ret) ans += i;
  cout << ans << endl;

  return 0;
}
0