#include #include using namespace std; const int WORD = 5005; int ch2v(char ch) { return ch - 'A' + 1; } string txt; int nWord; string word[WORD]; void read() { cin >> txt; cin >> nWord; for (int i = 0; i < nWord; ++i) { cin >> word[i]; } } void work() { map hash2cnt; for (int len = 1; len <= min(10, (int) txt.size()); ++len) { long long currHash = 0; for (int i = 0; i < len; ++i) { currHash = currHash * 26 + ch2v(txt[i]); } long long p = 1; for (int i = 0; i < len; ++i) { p *= 26; } for (int i = 0; i + len <= txt.size(); ++i) { if (hash2cnt.count(currHash)) { int old = hash2cnt[currHash]; hash2cnt[currHash] = old + 1; } else { hash2cnt[currHash] = 1; } currHash = currHash * 26 + ch2v(txt[i + len]) - p * ch2v(txt[i]); } } int sum = 0; for (int i = 0; i < nWord; ++i) { long long targetHash = 0; for (int j = 0; j < word[i].size(); ++j) { targetHash = targetHash * 26 + ch2v(word[i][j]); } sum += hash2cnt[targetHash]; } cout << sum << endl; } int main() { read(); work(); return 0; }