#include using namespace std; using ll = long long; class Aho_Corasick { struct node { node *no; vector next; vector matched; node() : no(nullptr), next(128, nullptr) {} ~node() { for (auto ite : next) if (ite != nullptr) delete ite; } }; vector unite(const vector& a, const vector& b) { vector res; set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res; } int K; node *root; public: Aho_Corasick(const vector& Ts) : K(Ts.size()), root(new node) { node *now; root->no = root; for (int i = 0; i < K; i++) { auto &T = Ts[i]; now = root; for (auto c : T) { if (now->next[c] == nullptr) { now->next[c] = new node; } now = now->next[c]; } now->matched.push_back(i); } queue q; for (int i = 0; i < 128; i++) { if (root->next[i] == nullptr) { root->next[i] = root; } else { root->next[i]->no = root; q.push(root->next[i]); } } while (!q.empty()) { now = q.front(); q.pop(); for (int i = 0; i < 128; i++) { if (now->next[i] != nullptr) { node *nx = now->no; while (nx->next[i] == nullptr) { nx = nx->no; } now->next[i]->no = nx->next[i]; now->next[i]->matched = unite(now->next[i]->matched, nx->next[i]->matched); q.push(now->next[i]); } } } } vector count(const string& S) { vector res(K); node *now = root; for (auto c : S) { while (now->next[c] == nullptr) { now = now->no; } now = now->next[c]; for (auto k : now->matched) { res[k]++; } } return res; } }; int main() { string S; int M; cin >> S >> M; vector C(M); for (int i = 0; i < M; i++) { cin >> C[i]; } Aho_Corasick aho(C); auto cnt = aho.count(S); ll res = 0; for (auto t : cnt) { res += t; } cout << res << endl; return 0; }