#define PROBLEM "https://yukicoder.me/problems/no/430" #ifndef STRING_AHO_CORASICK_HPP #define STRING_AHO_CORASICK_HPP 1 #include #include #include #include #include #include #ifndef DATA_STRUCTURE_TRIE_HPP #define DATA_STRUCTURE_TRIE_HPP 1 #include #include #include #include #include namespace kk2 { template struct TrieNode { int nxt[char_size]; int exist; std::vector accept; TrieNode() : exist(0) { std::fill(nxt, nxt + char_size, -1); } }; template struct Trie { using Node = TrieNode; std::vector nodes; constexpr static int root = 0; Trie() { nodes.emplace_back(); } int push_node() { nodes.emplace_back(); return (int)nodes.size() - 1; } void update_direct(int node, int id) { nodes[node].accept.push_back(id); } void update_child(int node) { ++nodes[node].exist; } void add(const std::string &str) { assert(!str.empty()); const int id = nodes[root].exist; int now = root; for (int i = 0; i < (int)str.size(); ++i) { const int d = str[i] - margin; if (nodes[now].nxt[d] == -1) nodes[now].nxt[d] = push_node(); update_child(now); now = nodes[now].nxt[d]; } update_direct(now, id); } template void query(const std::string &str) { query(str, [](int idx) { f(idx); }); } template void query(const std::string &str, const F &f) { int now = root; for (char c : str) { for (int &idx : nodes[now].accept) f(idx); const int d = c - margin; now = nodes[now].nxt[d]; if (now == -1) return; } for (int idx : nodes[now].accept) f(idx); } int count() const { return nodes[0].exist; } int size() const { return (int)nodes.size(); } // return the number of strings which have the prefix // corresponding to the node_id int size(int node_idx) const { return (int)nodes[node_idx].accept.size() + nodes[node_idx].exist; } }; } // namespace kk2 #endif // DATA_STRUCTURE_TRIE_HPP // #include "../data_structure/trie.hpp" namespace kk2 { template struct AhoCorasick : Trie { using Trie::Trie; using Trie::count; constexpr static int FAIL = char_size; std::vector correct, perm; void build() { correct.resize(this->size()); int now = 0; perm.resize(this->size()); perm[now++] = this->root; for (int i = 0; i < (int)this->size(); ++i) { correct[i] = (int)this->nodes[i].accept.size(); } std::queue que; for (int i = 0; i <= char_size; ++i) { if (this->nodes[this->root].nxt[i] == -1) { this->nodes[this->root].nxt[i] = this->root; } else { this->nodes[this->nodes[this->root].nxt[i]].nxt[FAIL] = this->root; que.emplace(this->nodes[this->root].nxt[i]); } } while (!que.empty()) { perm[now++] = que.front(); 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] == -1) { now.nxt[i] = this->nodes[fail].nxt[i]; } else { this->nodes[now.nxt[i]].nxt[FAIL] = this->nodes[fail].nxt[i]; que.emplace(now.nxt[i]); } } } } long long all_match(const std::string &str, int now_ = 0) { std::unordered_map visit_cnt; for (char c : str) { now_ = this->nodes[now_].nxt[c - margin]; visit_cnt[now_]++; } long long res{}; for (auto &&[now, cnt] : visit_cnt) { res += (long long)correct[now] * cnt; } return res; } std::vector each_match(const std::string &str, int now_ = 0) { std::vector visit_cnt(this->size()); for (char c : str) { now_ = this->nodes[now_].nxt[c - margin]; visit_cnt[now_]++; } std::vector res(this->count()); for (int i = this->size() - 1; i > 0; --i) { int now = perm[i]; visit_cnt[this->nodes[now].nxt[FAIL]] += visit_cnt[now]; for (int idx : this->nodes[now].accept) { res[idx] += visit_cnt[now]; } } return res; } int move(int now, char c) { return this->nodes[now].nxt[c - margin]; } int count(int node) const { return correct[node]; } }; } // namespace kk2 #endif // STRING_AHO_CORASICK_HPP // #include "../../string/aho_corasick.hpp" #ifndef TEMPLATE #define TEMPLATE 1 // #pragma GCC optimize("O3,unroll-loops") // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef TEMPLATE_FASTIO_HPP #define TEMPLATE_FASTIO_HPP 1 #include #include #include #include #include #include #ifndef TYPE_TRAITS_HPP #define TYPE_TRAITS_HPP 1 #include #include #include namespace kk2 { template using is_signed_int128 = typename std::conditional::value or std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value or std::is_same::value, std::true_type, std::false_type>::type; template using is_integral = typename std::conditional::value or is_signed_int128::value or is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed = typename std::conditional::value or is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned = typename std::conditional::value or is_unsigned_int128::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using to_unsigned = typename std::conditional::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; template using is_integral_t = std::enable_if_t::value>; template using is_signed_t = std::enable_if_t::value>; template using is_unsigned_t = std::enable_if_t::value>; template using is_function_pointer = typename std::conditional && std::is_function_v>, std::true_type, std::false_type>::type; template ::value> * = nullptr> struct is_two_args_function_pointer : std::false_type {}; template struct is_two_args_function_pointer : std::true_type {}; template using is_two_args_function_pointer_t = std::enable_if_t::value>; namespace type_traits { struct istream_tag {}; struct ostream_tag {}; } // namespace type_traits template using is_standard_istream = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_standard_ostream = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_user_defined_istream = std::is_base_of; template using is_user_defined_ostream = std::is_base_of; template using is_istream = typename std::conditional::value || is_user_defined_istream::value, std::true_type, std::false_type>::type; template using is_ostream = typename std::conditional::value || is_user_defined_ostream::value, std::true_type, std::false_type>::type; template using is_istream_t = std::enable_if_t::value>; template using is_ostream_t = std::enable_if_t::value>; } // namespace kk2 #endif // TYPE_TRAITS_HPP // #include "../type_traits/type_traits.hpp" namespace kk2 { namespace fastio { #define INPUT_FILE "in.txt" #define OUTPUT_FILE "out.txt" struct Scanner : type_traits::istream_tag { private: static constexpr size_t INPUT_BUF = 1 << 17; size_t pos = 0, end = 0; static char buf[INPUT_BUF]; FILE *fp; public: Scanner() : fp(stdin) {} Scanner(const char *file) : fp(fopen(file, "r")) {} ~Scanner() { if (fp != stdin) fclose(fp); } char now() { if (pos == end) { while (!(end = fread(buf, 1, INPUT_BUF, fp))) {} if (end != INPUT_BUF) buf[end] = '\0'; pos = 0; } return buf[pos]; } void skip_space() { while (isspace(now())) ++pos; } template * = nullptr> T next_unsigned_integral() { skip_space(); T res{}; while (isdigit(now())) { res = res * 10 + (now() - '0'); ++pos; } return res; } template * = nullptr> T next_signed_integral() { skip_space(); if (now() == '-') { ++pos; return T(-next_unsigned_integral::type>()); } else return (T)next_unsigned_integral::type>(); } char next_char() { skip_space(); auto res = now(); ++pos; return res; } std::string next_string() { skip_space(); std::string res; while (true) { char c = now(); if (isspace(c) or c == '\0') break; res.push_back(now()); ++pos; } return res; } template * = nullptr> Scanner &operator>>(T &x) { x = next_unsigned_integral(); return *this; } template * = nullptr> Scanner &operator>>(T &x) { x = next_signed_integral(); return *this; } Scanner &operator>>(char &x) { x = next_char(); return *this; } Scanner &operator>>(std::string &x) { x = next_string(); return *this; } }; struct endl_struct_t {}; struct Printer : type_traits::ostream_tag { private: static char helper[10000][5]; static char leading_zero[10000][5]; constexpr static size_t OUTPUT_BUF = 1 << 17; static char buf[OUTPUT_BUF]; size_t pos = 0; FILE *fp; template static constexpr void div_mod(T &a, T &b, T mod) { a = b / mod; b -= a * mod; } static void init() { buf[0] = '\0'; for (size_t i = 0; i < 10000; ++i) { leading_zero[i][0] = i / 1000 + '0'; leading_zero[i][1] = i / 100 % 10 + '0'; leading_zero[i][2] = i / 10 % 10 + '0'; leading_zero[i][3] = i % 10 + '0'; leading_zero[i][4] = '\0'; size_t j = 0; if (i >= 1000) helper[i][j++] = i / 1000 + '0'; if (i >= 100) helper[i][j++] = i / 100 % 10 + '0'; if (i >= 10) helper[i][j++] = i / 10 % 10 + '0'; helper[i][j++] = i % 10 + '0'; helper[i][j] = '\0'; } } public: Printer() : fp(stdout) { init(); } Printer(const char *file) : fp(fopen(file, "w")) { init(); } ~Printer() { write(); if (fp != stdout) fclose(fp); } void write() { fwrite(buf, 1, pos, fp); pos = 0; } void flush() { write(); fflush(fp); } void put_char(char c) { if (pos == OUTPUT_BUF) write(); buf[pos++] = c; } void put_cstr(const char *s) { while (*s) put_char(*(s++)); } void put_u32(uint32_t x) { uint32_t y; if (x >= 100000000) { // 10^8 div_mod(y, x, 100000000); put_cstr(helper[y]); div_mod(y, x, 10000); put_cstr(leading_zero[y]); put_cstr(leading_zero[x]); } else if (x >= 10000) { // 10^4 div_mod(y, x, 10000); put_cstr(helper[y]); put_cstr(leading_zero[x]); } else put_cstr(helper[x]); } void put_i32(int32_t x) { if (x < 0) { put_char('-'); put_u32(-x); } else put_u32(x); } void put_u64(uint64_t x) { uint64_t y; if (x >= 1000000000000ull) { // 10^12 div_mod(y, x, 1000000000000ull); put_u32(y); div_mod(y, x, 100000000ull); put_cstr(leading_zero[y]); div_mod(y, x, 10000ull); put_cstr(leading_zero[y]); put_cstr(leading_zero[x]); } else if (x >= 10000ull) { // 10^4 div_mod(y, x, 10000ull); put_u32(y); put_cstr(leading_zero[x]); } else put_cstr(helper[x]); } void put_i64(int64_t x) { if (x < 0) { put_char('-'); put_u64(-x); } else put_u64(x); } void put_u128(__uint128_t x) { constexpr static __uint128_t pow10_10 = 10000000000ull; constexpr static __uint128_t pow10_20 = pow10_10 * pow10_10; __uint128_t y; if (x >= pow10_20) { // 10^20 div_mod<__uint128_t>(y, x, pow10_20); put_u64(uint64_t(y)); div_mod<__uint128_t>(y, x, __uint128_t(10000000000000000ull)); put_cstr(leading_zero[y]); div_mod<__uint128_t>(y, x, __uint128_t(1000000000000ull)); put_cstr(leading_zero[y]); div_mod<__uint128_t>(y, x, __uint128_t(100000000ull)); put_cstr(leading_zero[y]); div_mod<__uint128_t>(y, x, __uint128_t(10000ull)); put_cstr(leading_zero[y]); put_cstr(leading_zero[x]); } else if (x >= __uint128_t(10000)) { // 10^4 div_mod<__uint128_t>(y, x, __uint128_t(10000)); put_u64(uint64_t(y)); put_cstr(leading_zero[x]); } else put_cstr(helper[x]); } void put_i128(__int128_t x) { if (x < 0) { put_char('-'); put_u128(-x); } else put_u128(x); } template * = nullptr> Printer &operator<<(T x) { if constexpr (sizeof(T) <= 4) put_u32(x); else if constexpr (sizeof(T) <= 8) put_u64(x); else put_u128(x); return *this; } template * = nullptr> Printer &operator<<(T x) { if constexpr (sizeof(T) <= 4) put_i32(x); else if constexpr (sizeof(T) <= 8) put_i64(x); else put_i128(x); return *this; } Printer &operator<<(char x) { put_char(x); return *this; } Printer &operator<<(const std::string &x) { for (char c : x) put_char(c); return *this; } Printer &operator<<(const char *x) { put_cstr(x); return *this; } // std::cout << std::endl; は関数ポインタを渡しているらしい Printer &operator<<(endl_struct_t) { put_char('\n'); flush(); return *this; } }; char Scanner::buf[Scanner::INPUT_BUF]; char Printer::buf[Printer::OUTPUT_BUF]; char Printer::helper[10000][5]; char Printer::leading_zero[10000][5]; } // namespace fastio #if defined(INTERACTIVE) || defined(USE_STDIO) #ifdef KK2 std::ifstream kin(INPUT_FILE); std::ofstream kout(OUTPUT_FILE); auto (*kendl)(std::ostream &) = std::endl>; #else auto &kin = std::cin; auto &kout = std::cout; auto (*kendl)(std::ostream &) = std::endl>; #endif #elif defined(KK2) fastio::Scanner kin(INPUT_FILE); fastio::Printer kout(OUTPUT_FILE); fastio::endl_struct_t kendl; #else fastio::Scanner kin; fastio::Printer kout; fastio::endl_struct_t kendl; #endif } // namespace kk2 #endif // TEMPLATE_FASTIO_HPP // #include "fastio.hpp" #ifndef TEMPLATE_TYPE_ALIAS_HPP #define TEMPLATE_TYPE_ALIAS_HPP 1 #include #include #include #include using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using pi = std::pair; using pl = std::pair; using pil = std::pair; using pli = std::pair; template using vc = std::vector; template using vvc = std::vector>; template using vvvc = std::vector>; template using vvvvc = std::vector>; template using pq = std::priority_queue; template using pqi = std::priority_queue, std::greater>; #endif // TEMPLATE_TYPE_ALIAS_HPP // #include "type_alias.hpp" #ifndef TEMPLATE_CONSTANT_HPP #define TEMPLATE_CONSTANT_HPP 1 template constexpr T infty = 0; template <> constexpr int infty = (1 << 30) - 123; template <> constexpr i64 infty = (1ll << 62) - (1ll << 31); template <> constexpr i128 infty = (i128(1) << 126) - (i128(1) << 63); template <> constexpr u32 infty = infty; template <> constexpr u64 infty = infty; template <> constexpr u128 infty = infty; template <> constexpr double infty = infty; template <> constexpr long double infty = infty; constexpr int mod = 998244353; constexpr int modu = 1e9 + 7; constexpr long double PI = 3.14159265358979323846; #endif // TEMPLATE_CONSTANT_HPP // #include "constant.hpp" #ifndef TEMPLATE_FUNCTIONAL_UTIL_HPP #define TEMPLATE_FUNCTIONAL_UTIL_HPP 1 namespace kk2 { template auto make_vector(int first, Sizes... sizes) { if constexpr (sizeof...(sizes) == 0) { return std::vector(first); } else { return std::vector(sizes...))>(first, make_vector(sizes...)); } } template void fill_all(std::vector &v, const U &x) { std::fill(std::begin(v), std::end(v), T(x)); } template void fill_all(std::vector> &v, const U &x) { for (auto &u : v) fill_all(u, x); } } // namespace kk2 template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } #endif // TEMPLATE_FUNCTIONAL_UTIL_HPP // #include "function_util.hpp" #ifndef TEMPLATE_MACROS_HPP #define TEMPLATE_MACROS_HPP 1 #define rep1(a) for (long long _ = 0; _ < (long long)(a); ++_) #define rep2(i, a) for (long long i = 0; i < (long long)(a); ++i) #define rep3(i, a, b) for (long long i = (a); i < (long long)(b); ++i) #define repi2(i, a) for (long long i = (a) - 1; i >= 0; --i) #define repi3(i, a, b) for (long long i = (a) - 1; i >= (long long)(b); --i) #define overload3(a, b, c, d, ...) d #define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define repi(...) overload3(__VA_ARGS__, repi3, repi2, rep1)(__VA_ARGS__) #define fi first #define se second #define all(p) p.begin(), p.end() #endif // TEMPLATE_MACROS_HPP // #include "macros.hpp" #ifndef TEMPLATE_IO_UTIL_HPP #define TEMPLATE_IO_UTIL_HPP 1 #include #include #include // #include "../type_traits/type_traits.hpp" template * = nullptr> IStream &operator>>(IStream &is, std::pair &p) { is >> p.first >> p.second; return is; } template * = nullptr> OStream &operator<<(OStream &os, const std::pair &p) { os << p.first << ' ' << p.second; return os; } template * = nullptr> IStream &operator>>(IStream &is, std::vector &v) { for (auto &x : v) is >> x; return is; } template * = nullptr> OStream &operator<<(OStream &os, const std::vector &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 == (int)v.size() ? "" : " "); } return os; } template * = nullptr> IStream &operator>>(IStream &is, std::array &a) { for (auto &x : a) is >> x; return is; } template * = nullptr> OStream &operator<<(OStream &os, const std::array &a) { for (int i = 0; i < (int)F; i++) { os << a[i] << (i + 1 == (int)F ? "" : " "); } return os; } #endif // TEMPLATE_IO_UTIL_HPP // #include "io_util.hpp" using kk2::kendl; using kk2::kin; using kk2::kout; void Yes(bool b = 1) { kout << (b ? "Yes\n" : "No\n"); } void No(bool b = 1) { kout << (b ? "No\n" : "Yes\n"); } void YES(bool b = 1) { kout << (b ? "YES\n" : "NO\n"); } void NO(bool b = 1) { kout << (b ? "NO\n" : "YES\n"); } void yes(bool b = 1) { kout << (b ? "yes\n" : "no\n"); } void no(bool b = 1) { kout << (b ? "no\n" : "yes\n"); } #endif // TEMPLATE // #include "../../template/template.hpp" using namespace std; int main() { string s; kin >> s; int m; kin >> m; vc c(m); kin >> c; kk2::AhoCorasick<26, 'A'> ac; for (auto &x : c) ac.add(x); ac.build(); auto each = ac.each_match(s); i64 res = accumulate(all(each), 0LL); kout << res << kendl; return 0; } // converted!!