#include #include using namespace std; using namespace atcoder; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair; using pdd = pair; using pll = pair; using pli = pair; using pil = pair; template using Graph = vector>; const int MOD = 1e9 + 7; const ld PI = acos(-1); template struct Trie { struct Node { Node *next[char_size]; int exist; vector accept; Node() : exist(0) { memset(next, 0, sizeof(next)); } ~Node() { for (int i = 0; i < char_size; ++i) { if (next[i] != nullptr) { delete next[i]; } } } }; Node *root; int size; Trie() : root(new Node), size(1) {} void add(const string &str, int id) { Node *now = root; for (int i = 0; i < str.size(); ++i) { int c = str[i] - base; if (now->next[c] == nullptr) { now->next[c] = new Node; size++; } (now->exist)++; now = now->next[c]; } (now->exist)++; now->accept.emplace_back(id); } void add(const string &str) { add(str, root->exist); } bool query(const string &str, bool prefix = false) { Node *now = root; for (int i = 0; i < str.size(); ++i) { int c = str[i] - base; if (now->next[c] == nullptr) return false; now = now->next[c]; } return prefix | (now->accept.size() > 0); } int count() const { return root->exist; } }; template struct AhoCorasick : Trie { using typename Trie::Node; const int FAIL = char_size; void build() { queue que; for (int i = 0; i <= char_size; ++i) { Node *&next_node = this->root->next[i]; if (next_node != nullptr) { next_node->next[FAIL] = this->root; que.emplace(next_node); } else { next_node = this->root; } } while (!que.empty()) { Node *now = que.front(); Node *fail_node = now->next[FAIL]; que.pop(); for (int i = 0; i < char_size; ++i) { if (now->next[i] == nullptr) { now->next[i] = fail_node->next[i]; continue; } while (fail_node->next[i] == nullptr) fail_node = fail_node->next[FAIL]; now->next[i]->next[FAIL] = fail_node->next[i]; vector accept; vector &u = now->next[i]->accept; vector &v = fail_node->next[i]->accept; set_union(u.begin(), u.end(), v.begin(), v.end(), back_inserter(accept)); swap(u, accept); que.push(now->next[i]); } } } vector match(const string &str) { Node *now = this->root; vector res(this->count()); for (int i = 0; i < str.size(); ++i) { int c = str[i] - base; now = now->next[c]; for (int j = 0; j < now->accept.size(); ++j) { res[now->accept[j]]++; } } return res; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); string S; cin >> S; int M; cin >> M; AhoCorasick<26, 'A'> ahc; for (int i = 0; i < M; ++i) { string C; cin >> C; ahc.add(C); } ahc.build(); auto res = ahc.match(S); ll ans = 0; for (auto i : res) { ans += i; } cout << ans << endl; return 0; }