#include #include #include #include #include #include #include using namespace std; // Aに0を割り当てるとAAAとかでおかしくなるので、1を割り当てる const int N_CHAR = 27; string S; map hashes; long long calc_hash(string t) { long long x = 0; for (char c: t) { x = x * N_CHAR + (c - 'A' + 1); } return x; } void make_hashes(int len) { // printf("---make_hashes: len %d\n", len); if (len > S.size()) { return; } long long mod = 1; for (int i = 0; i < len; i++) { mod *= N_CHAR; } auto t = S.substr(0, len); // cout << t << endl; auto x = calc_hash(t); hashes[x]++; // printf("hash find: %15lld\n", x); for (int i = t.size(); i < S.size(); i++) { x = x * N_CHAR % mod + (S[i] - 'A' + 1); // printf("hash find: %15lld\n", x); hashes[x]++; } } int main() { string c; cin >> S; for (int i = 1; i <= 10; i++) { make_hashes(i); } int ans = 0; int m; cin >> m; for (int i = 0; i < m; i++) { cin >> c; auto x = calc_hash(c); ans += hashes[x]; // printf("c %10s: hash %15lld, add %3d\n", c.c_str(), x, hashes[x]); } cout << ans << endl; }