//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 0xccccccc; const ll LINF = 922337203685477580LL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} template struct Trie { struct Node { vector nex, accept; int c, common; Node(int c_) : c(c_), common(0) { nex.assign(char_size, -1); } }; vector nodes; int root; Trie() : root(0) { nodes.assign(1, Node(root)); } void insert(const string &word, int word_id) { int node_id = 0; for(int i = 0; i < (int)word.size(); i++) { int c = word[i] - base; int &next_id = nodes[node_id].nex[c]; if(next_id == -1) { next_id = nodes.size(); nodes.emplace_back(Node(c)); } ++nodes[node_id].common; node_id = next_id; } ++nodes[node_id].common; nodes[node_id].accept.emplace_back(word_id); } inline void insert(const string word) { insert(word, nodes[root].common); } int search(const string word, bool prefix = false) { int node_id = 0; for(int i = 0; i < (int)word.size(); i++) { int c = word[i] - base; int &next_id = nodes[node_id].nex[c]; if(next_id == -1) { return 0; } node_id = next_id; } return prefix ? 1 : (int)nodes[node_id].accept.size(); } inline int start_with(const string &prefix) { return search(prefix, true); } int count() const { return nodes[0].common; } size_t size() const { return nodes.size(); } }; //head string s; int m; Trie<26, 'A'> trie; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s >> m; rep(i, s.size()) { rep(j, 10) { if(i+j < s.size()) { trie.insert(s.substr(i, j+1)); } } } ll res = 0; rep(i, m) { cin >> s; res += trie.search(s); } cout << res << endl; }