#include #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" #define NDEBUG using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template constexpr T popcount(const T u) { return u ? static_cast(__builtin_popcountll(static_cast(u))) : static_cast(0); } template constexpr T log2p1(const T u) { return u ? static_cast(64 - __builtin_clzll(static_cast(u))) : static_cast(0); } template constexpr T msbp1(const T u) { return log2p1(u); } template constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast(u); } template constexpr bool ispow2(const T u) { return u and (static_cast(u) & static_cast(u - 1)) == 0; } template constexpr T ceil2(const T u) { return static_cast(1) << clog(u); } template constexpr T floor2(const T u) { return u == 0 ? static_cast(0) : static_cast(1) << (log2p1(u) - 1); } template constexpr bool btest(const T mask, const usize ind) { return ((static_cast(mask) >> ind) & static_cast(1)); } template constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast(0) : static_cast((static_cast(mask) << (64 - ind)) >> (64 - ind)); } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template T read() { T v; return std::cin >> v, v; } template std::vector read_vec(const std::size_t size) { std::vector v(size); for (auto& e : v) { std::cin >> e; } return v; } template auto read_vals() { return std::tuple...>{read()...}; } #define SHOW(...) static_cast(0) template std::vector make_v(const std::size_t size, T v) { return std::vector(size, v); } template auto make_v(const std::size_t size, Args... args) { return std::vector(size, make_v(args...)); } class trie { public: struct node_t { usize sub = 0; std::vector accept; std::map child; }; trie() : nodes{node_t{}} {} const node_t& operator[](const usize ind) const { return nodes[ind]; } void add(const std::string& s) { const usize sind = nodes[0].sub++; for (usize v = 0, i = 0; i < s.size(); i++) { const char c = s[i]; if (nodes[v].child.find(c) == nodes[v].child.end()) { nodes[v].child[c] = new_node(); } v = nodes[v].child.at(c), nodes[v].sub++; if (i + 1 == s.size()) { nodes[v].accept.push_back(sind); } } } std::vector path_to(const std::string& s) const { std::vector ans{0}; for (usize v = 0, i = 0; i < s.size(); i++) { const char c = s[i]; if (nodes[v].child.find(c) == nodes[v].child.end()) { break; } v = nodes[v].child.at(c), ans.push_back(v); } return ans; } friend std::ostream& operator<<(std::ostream& os, const trie& tr) { auto rec = [&](auto&& self, const usize v, const char c) -> void { os << "(" << c; for (const auto& q : tr.nodes[v].child) { self(self, q.second, q.first); } os << ")"; }; return rec(rec, 0, ' '), os; } usize count() const { return nodes.front().sub; } usize size() const { return nodes.size(); } private: usize new_node() { return nodes.push_back(node_t{}), nodes.size() - 1; } std::vector nodes; }; class aho_corasick { public: aho_corasick(const trie& tr) : tr{tr}, accept(tr.size()), failure(accept.size(), accept.size()) { const usize sz = accept.size(); std::queue q; std::string label(sz, ' '); std::vector par(sz, sz); q.push(0); while (not q.empty()) { const usize v = q.front(); q.pop(); for (const auto& p : tr[v].child) { label[p.second] = p.first, par[p.second] = v, q.push(p.second); } if (v == 0) { continue; } usize u = failure[par[v]]; const char c = label[v]; for (; u < sz and tr[u].child.find(c) == tr[u].child.end(); u = failure[u]) {} failure[v] = (u == sz ? 0UL : tr[u].child.at(c)); } } usize operator[](const usize i) const { return failure[i]; } std::vector> match(const std::string& s, const bool skip_precalc = false) // 末尾位置 { if (not skip_precalc and not precalced) { precalc(); } const usize sz = tr.size(); std::vector> ans(tr.count()); for (usize v = 0, i = 0; i < s.size(); i++) { const char c = s[i]; for (; v < sz and tr[v].child.find(c) == tr[v].child.end(); v = failure[v]) {} v = (v == sz ? 0UL : tr[v].child.at(c)); std::cerr << v << std::endl; for (const usize si : accept[v]) { ans[si].push_back(i); } } return ans; } private: void precalc() { precalced = true; std::queue q; q.push(0); while (not q.empty()) { const usize v = q.front(), f = failure[v]; q.pop(); for (const auto& p : tr[v].child) { q.push(p.second); } if (v == 0) { continue; } std::set_union(tr[v].accept.begin(), tr[v].accept.end(), accept[f].begin(), accept[f].end(), std::back_inserter(accept[v])); } } const trie& tr; bool precalced = false; std::vector> accept; std::vector failure; }; int main() { const auto s = read(); const auto n = read(); const auto c = read_vec(n); trie tr; for (usize i = 0; i < n; i++) { tr.add(c[i]); } aho_corasick aho(tr); const auto p = aho.match(s); usize ans = 0; for (usize i = 0; i < n; i++) { ans += p[i].size(); } std::cout << ans << std::endl; return 0; }