結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  emthrm | 
| 提出日時 | 2020-01-03 00:35:17 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 30 ms / 2,000 ms | 
| コード長 | 4,093 bytes | 
| コンパイル時間 | 3,399 ms | 
| コンパイル使用メモリ | 209,984 KB | 
| 最終ジャッジ日時 | 2025-01-08 15:56:48 | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
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()
using ll = long long;
template <typename T> using posteriority_queue = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
int popcount(int val) { return __builtin_popcount(val); }
int popcountll(ll val) { return __builtin_popcountll(val); }
template <typename T> void unique(vector<T> &a) { a.erase(unique(ALL(a)), a.end()); }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;
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(char basis = 'a') : basis(basis) { nodes.emplace_back('$'); }
  void add(const string &s, int id = -1, int pos = 0) {
    for (char c : s) {
      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 - basis; }
private:
  char basis;
};
template <size_t char_sz = 26>
struct AhoCorasick : Trie<char_sz + 1> {
  using Trie<char_sz + 1>::Trie;
  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];
  }
  int match(const string &t, int pos = 0) {
    int total = 0;
    for (char c : t) {
      pos = move(c, pos);
      total += this->nodes[pos].tails.size();
    }
    return total;
  }
  map<int, int> match_heavy(const string &t, int pos = 0) {
    map<int, int> mp;
    for (char c : t) {
      pos = move(c, pos);
      for (int e : this->nodes[pos].tails) ++mp[e];
    }
    return mp;
  }
};
int main() {
  string s; cin >> s;
  AhoCorasick<> aho('A');
  int m; cin >> m;
  REP(i, m) {
    string p; cin >> p;
    aho.add(p, i);
  }
  aho.build();
  cout << aho.match(s) << '\n';
  return 0;
}
            
            
            
        