#include #define rep(x, to) for (int x = 0; x < (to); x++) #define REP(x, a, to) for (int x = (a); x < (to); x++) #define EPS (1e-14) #define _PA(x,N) rep(i,N){cout< PII; typedef pair PLL; typedef complex Complex; typedef vector< vector > Mat; string S; int M; string C[5005]; ll ans; struct RollingHash { string s; ull p; ull mod; ull phash[int(1e+6) + 5]; ull pow[int(1e+6) + 5]; void init(string t, ull q=1e+9 + 7, ull m=(1ULL<<30)) { s = t; p = q; mod = m; //memset(phash, 0, sizeof(phash)); //memset(pow, 0, sizeof(pow)); } void build() { phash[0] = 0; //1-indexed pow[0] = 1;// 0-indexed for (int i = 0; i < (int)s.size(); i++) { phash[i + 1] = (s[i]-'0') + phash[i] * p; phash[i + 1] %= mod; pow[i + 1] = p * pow[i]; pow[i + 1] %= mod; } } ull hash(int i, int j) { ull res = (phash[i] * pow[j - i]) % mod; return (phash[j] + mod - res) % mod; } size_t size() { return s.size(); } }; RollingHash rhs; RollingHash rhq; map freq; void solve() { rhs.init(S); rhs.build(); for (int i = 0; i < S.size(); i++) { for (int len = 1; len <= 10 && i + len <= S.size(); len++) { freq[rhs.hash(i, i + len)] += 1; } } for (int i = 0; i < M; i++) { rhq.init(C[i]); rhq.build(); ull hash1 = rhq.hash(0, C[i].size()); ans += freq[hash1]; } cout << ans << endl; } int main() { cin >> S; cin >> M; for (int i = 0; i < M; i++) { cin >> C[i]; } solve(); return 0; }