Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/yukicoder-430.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/430

#include <iostream>
#include <string>

#include "../../string/aho-corasick.hpp"

using namespace std;

int main() {
  string S;
  int M;
  cin >> S;
  cin >> M;
  AhoCorasick<26, 'A'> aho;
  for (int i = 0; i < M; i++) {
    string T;
    cin >> T;
    aho.add(T);
  }
  aho.build();
  cout << aho.move(S).first << endl;
}
#line 1 "test/verify/yukicoder-430.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/430

#include <iostream>
#include <string>

#line 2 "string/aho-corasick.hpp"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <queue>
#line 8 "string/aho-corasick.hpp"
#include <unordered_map>
#include <utility>
#include <vector>

#line 2 "structure/trie/trie.hpp"

#line 4 "structure/trie/trie.hpp"
#include <cassert>
#line 6 "structure/trie/trie.hpp"
#include <cstring>
#include <functional>
#line 10 "structure/trie/trie.hpp"

template <int char_size>
struct TrieNode {
  int nxt[char_size];

  int exist;
  std::vector<int> accept;

  TrieNode() : exist(0) { std::memset(nxt, -1, sizeof(nxt)); }
};

template <int char_size, int margin>
struct Trie {
  using Node = TrieNode<char_size>;

  std::vector<Node> nodes;
  int root;

  Trie() : root(0) { nodes.push_back(Node()); }

  void update_direct(int node, int id) { nodes[node].accept.push_back(id); }

  void update_child(int node, int child, int id) { ++nodes[node].exist; }

  void add(const std::string& str, int str_index, int node_index, int id) {
    if (str_index == str.size()) {
      update_direct(node_index, id);
    } else {
      const int c = str[str_index] - margin;
      if (nodes[node_index].nxt[c] == -1) {
        nodes[node_index].nxt[c] = (int)nodes.size();
        nodes.push_back(Node());
      }
      add(str, str_index + 1, nodes[node_index].nxt[c], id);
      update_child(node_index, nodes[node_index].nxt[c], id);
    }
  }

  void add(const std::string& str, int id) { add(str, 0, 0, id); }

  void add(const std::string& str) { add(str, nodes[0].exist); }

  void query(const std::string& str, const std::function<void(int)>& f,
             int str_index, int node_index) {
    for (auto& idx : nodes[node_index].accept) f(idx);
    if (str_index == str.size()) {
      return;
    } else {
      const int c = str[str_index] - margin;
      if (nodes[node_index].nxt[c] == -1) return;
      query(str, f, str_index + 1, nodes[node_index].nxt[c]);
    }
  }

  void query(const std::string& str, const std::function<void(int)>& f) {
    query(str, f, 0, 0);
  }

  int count() const { return (nodes[0].exist); }

  int size() const { return ((int)nodes.size()); }
};
#line 13 "string/aho-corasick.hpp"

template <int char_size, int margin>
struct AhoCorasick : Trie<char_size + 1, margin> {
  using Trie<char_size + 1, margin>::Trie;

  const int FAIL = char_size;
  std::vector<int> correct;

  void build(bool heavy = true) {
    correct.resize(this->size());
    for (int i = 0; i < this->size(); i++) {
      correct[i] = (int)this->nodes[i].accept.size();
    }
    std::queue<int> que;
    for (int i = 0; i <= char_size; i++) {
      if (~this->nodes[0].nxt[i]) {
        this->nodes[this->nodes[0].nxt[i]].nxt[FAIL] = 0;
        que.emplace(this->nodes[0].nxt[i]);
      } else {
        this->nodes[0].nxt[i] = 0;
      }
    }
    while (!que.empty()) {
      auto& now = this->nodes[que.front()];
      int fail = now.nxt[FAIL];
      correct[que.front()] += correct[fail];
      que.pop();
      for (int i = 0; i < char_size; i++) {
        if (~now.nxt[i]) {
          this->nodes[now.nxt[i]].nxt[FAIL] = this->nodes[fail].nxt[i];
          if (heavy) {
            auto& u = this->nodes[now.nxt[i]].accept;
            auto& v = this->nodes[this->nodes[fail].nxt[i]].accept;
            std::vector<int> accept;
            set_union(std::begin(u), std::end(u), std::begin(v), std::end(v),
                      std::back_inserter(accept));
            u = accept;
          }
          que.emplace(now.nxt[i]);
        } else {
          now.nxt[i] = this->nodes[fail].nxt[i];
        }
      }
    }
  }

  std::unordered_map<int, int> match(const std::string& str, int now = 0) {
    std::unordered_map<int, int> result, visit_cnt;
    for (auto& c : str) {
      now = this->nodes[now].nxt[c - margin];
      visit_cnt[now]++;
    }
    for (auto& [now, cnt] : visit_cnt) {
      for (auto& v : this->nodes[now].accept) result[v] += cnt;
    }
    return result;
  }

  std::pair<std::int64_t, int> move(const char& c, int now = 0) {
    now = this->nodes[now].nxt[c - margin];
    return {correct[now], now};
  }

  std::pair<std::int64_t, int> move(const std::string& str, int now = 0) {
    std::int64_t sum = 0;
    for (auto& c : str) {
      auto nxt = move(c, now);
      sum += nxt.first;
      now = nxt.second;
    }
    return {sum, now};
  }
};
#line 7 "test/verify/yukicoder-430.test.cpp"

using namespace std;

int main() {
  string S;
  int M;
  cin >> S;
  cin >> M;
  AhoCorasick<26, 'A'> aho;
  for (int i = 0; i < M; i++) {
    string T;
    cin >> T;
    aho.add(T);
  }
  aho.build();
  cout << aho.move(S).first << endl;
}

Test cases

Env Name Status Elapsed Memory
g++ challenge01 :heavy_check_mark: AC 2 ms 4 MB
g++ challenge02 :heavy_check_mark: AC 11 ms 9 MB
g++ challenge03 :heavy_check_mark: AC 5 ms 5 MB
g++ challenge04 :heavy_check_mark: AC 5 ms 5 MB
g++ sample1 :heavy_check_mark: AC 2 ms 4 MB
g++ sample2 :heavy_check_mark: AC 2 ms 4 MB
g++ sample3 :heavy_check_mark: AC 2 ms 4 MB
g++ sample4 :heavy_check_mark: AC 2 ms 4 MB
g++ test1 :heavy_check_mark: AC 3 ms 4 MB
g++ test10 :heavy_check_mark: AC 7 ms 6 MB
g++ test2 :heavy_check_mark: AC 2 ms 4 MB
g++ test3 :heavy_check_mark: AC 2 ms 4 MB
g++ test4 :heavy_check_mark: AC 8 ms 6 MB
g++ test5 :heavy_check_mark: AC 8 ms 6 MB
g++ test6 :heavy_check_mark: AC 7 ms 7 MB
g++ test7 :heavy_check_mark: AC 7 ms 6 MB
g++ test8 :heavy_check_mark: AC 7 ms 6 MB
g++ test9 :heavy_check_mark: AC 7 ms 6 MB
clang++ challenge01 :heavy_check_mark: AC 2 ms 4 MB
clang++ challenge02 :heavy_check_mark: AC 11 ms 9 MB
clang++ challenge03 :heavy_check_mark: AC 6 ms 5 MB
clang++ challenge04 :heavy_check_mark: AC 6 ms 5 MB
clang++ sample1 :heavy_check_mark: AC 2 ms 4 MB
clang++ sample2 :heavy_check_mark: AC 2 ms 4 MB
clang++ sample3 :heavy_check_mark: AC 2 ms 4 MB
clang++ sample4 :heavy_check_mark: AC 2 ms 4 MB
clang++ test1 :heavy_check_mark: AC 3 ms 4 MB
clang++ test10 :heavy_check_mark: AC 7 ms 6 MB
clang++ test2 :heavy_check_mark: AC 2 ms 4 MB
clang++ test3 :heavy_check_mark: AC 2 ms 4 MB
clang++ test4 :heavy_check_mark: AC 8 ms 6 MB
clang++ test5 :heavy_check_mark: AC 8 ms 6 MB
clang++ test6 :heavy_check_mark: AC 8 ms 6 MB
clang++ test7 :heavy_check_mark: AC 7 ms 7 MB
clang++ test8 :heavy_check_mark: AC 7 ms 6 MB
clang++ test9 :heavy_check_mark: AC 7 ms 6 MB
Back to top page