#define MOD_TYPE 1 #pragma region Macros #include using namespace std; #if 0 #include #include using Int = boost::multiprecision::cpp_int; using lld = boost::multiprecision::cpp_dec_float_100; #endif #if 1 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif using ll = long long int; using ld = long double; using pii = pair; using pll = pair; using pld = pair; template using smaller_queue = priority_queue, greater>; constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353); constexpr int INF = (int)1e9 + 10; constexpr ll LINF = (ll)4e18; constexpr ld PI = acos(-1.0); constexpr ld EPS = 1e-7; constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0}; #define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i) #define rep(i, n) REP(i, 0, n) #define REPI(i, m, n) for (int i = m; i < (int)(n); ++i) #define repi(i, n) REPI(i, 0, n) #define MP make_pair #define MT make_tuple #define YES(n) cout << ((n) ? "YES" : "NO") << "\n" #define Yes(n) cout << ((n) ? "Yes" : "No") << "\n" #define possible(n) cout << ((n) ? "possible" : "impossible") << "\n" #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n" #define all(v) v.begin(), v.end() #define NP(v) next_permutation(all(v)) #define dbg(x) cerr << #x << ":" << x << "\n"; struct io_init { io_init() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(30) << setiosflags(ios::fixed); }; } io_init; template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } template inline void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template constexpr istream &operator>>(istream &is, pair &p) noexcept { is >> p.first >> p.second; return is; } template constexpr ostream &operator<<(ostream &os, pair &p) noexcept { os << p.first << " " << p.second; return os; } #pragma endregion // -------------------------------------- template struct AhoCorasick { struct node { // suff : 先頭の文字を最小限消してグラフに存在する頂点にするときの行先の頂点 // dict : 先頭の文字を最小限消して辞書に存在する単語にするときの行先の頂点 // depth : Trie木における深さ(省略可能) // word_index : このノードで終わる単語のindex(祖先は含まない。なければ-1)(複数ある場合は最小のもの) // word_count : このノードで終わる単語の総数 // link : Trie及びsuffixの辺の接続先頂点(なければ-1) int suff = -1, dict = -1, depth = 0; int word_index = -1, word_count = 0; int link[ALPHABET]; node() { fill(link, link + ALPHABET, -1); } int &operator[](char c) { return link[c - MIN_CHAR]; } }; // nodes : 頂点集合 // W : 現在の単語数 // word_location : 各単語のTrie木の最後の頂点のindex // defer : 同じ単語が辞書内に存在する場合、最初の単語のindexを記録 vector nodes; int W; vector word_location; vector word_indices_by_depth; vector defer; AhoCorasick(){}; AhoCorasick(const vector &words = {}) { build(words); } // suffixを親とする木の隣接リスト これの上でDPやクエリ処理を行うことが多い vector> build_suffix_adj() const { vector> adj(nodes.size()); for (int i = 1; i < int(nodes.size()); i++) adj[nodes[i].suff].push_back(i); return adj; } int get_or_add_child(int current, char c) { if (nodes[current][c] >= 0) return nodes[current][c]; int index = int(nodes.size()); nodes[current][c] = index; nodes.emplace_back(); nodes.back().depth = nodes[current].depth + 1; return index; } int add_word(const string &word, int word_index) { assert(!nodes.empty()); int current = 0; for (char c : word) current = get_or_add_child(current, c); if (nodes[current].word_index < 0) nodes[current].word_index = word_index; nodes[current].word_count++; return current; } // locationからcを追加したときの行き先 O(1) int get_suffix_link(int location, char c) const { if (location >= 0) location = nodes[location].link[c - MIN_CHAR]; return max(location, 0); } void build(const vector &words) { nodes = {node()}; W = int(words.size()); word_location.resize(W); defer.resize(W); int max_depth = 0; for (int i = 0; i < W; i++) { word_location[i] = add_word(words[i], i); max_depth = max(max_depth, int(words[i].size())); defer[i] = nodes[word_location[i]].word_index; } // depthの降順に単語indexのリストを作成 word_indices_by_depth.resize(W); vector depth_freq(max_depth + 1, 0); for (int i = 0; i < W; i++) depth_freq[words[i].size()]++; for (int i = max_depth - 1; i >= 0; i--) depth_freq[i] += depth_freq[i + 1]; for (int i = 0; i < W; i++) word_indices_by_depth[--depth_freq[words[i].size()]] = i; // depth順のBFSでsuffix parentを求める vector q = {0}; for (int i = 0; i < int(q.size()); i++) { int current = q[i]; for (char c = MIN_CHAR; c < MIN_CHAR + ALPHABET; c++) { int &index = nodes[current][c]; if (index >= 0) { // currentのsuffix parentで子cを持つものが見つかるまで走査して // indexのsuffix parentを見つける int suffix_parent = get_suffix_link(nodes[current].suff, c); nodes[index].suff = suffix_parent; nodes[index].word_count += nodes[suffix_parent].word_count; nodes[index].dict = nodes[suffix_parent].word_index < 0 ? nodes[suffix_parent].dict : suffix_parent; q.push_back(index); } else { index = get_suffix_link(nodes[current].suff, c); } } } } // 辞書内のそれぞれの単語がtextに何個含まれているか O(text length + num words) vector count_matches(const string &text) const { vector matches(W, 0); int current = 0; for (char c : text) { current = get_suffix_link(current, c); int dict_node = nodes[current].word_index < 0 ? nodes[current].dict : current; if (dict_node >= 0) matches[nodes[dict_node].word_index]++; } // depthの降順に見る for (int word_index : word_indices_by_depth) { int location = word_location[word_index]; int dict_node = nodes[location].dict; if (dict_node >= 0) matches[nodes[dict_node].word_index] += matches[word_index]; } for (int i = 0; i < W; i++) matches[i] = matches[defer[i]]; return matches; } // textに含まれる辞書内の単語で、textのi文字目で終わるものの個数 O(text length) vector count_matches_by_position(const string &text) const { vector matches(text.size()); int current = 0; for (int i = 0; i < int(text.size()); i++) { current = get_suffix_link(current, text[i]); matches[i] = nodes[current].word_count; } return matches; } // textに辞書内の単語が合計何個含まれているか O(text length) int64_t count_total_matches(const string &text) const { int64_t matches = 0; int current = 0; for (char c : text) { current = get_suffix_link(current, c); matches += nodes[current].word_count; } return matches; } }; void solve() { string s; cin >> s; int m; cin >> m; vector v(m); rep(i, m) cin >> v[i]; AhoCorasick<'A', 26> aho(v); cout << aho.count_total_matches(s) << "\n"; } int main() { solve(); }