#include "bits/stdc++.h" #include using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair plint; typedef pair pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define endk '\n' templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } const lint mod = 1e9 + 7, INF = 1e18; lint dx[8] = { 0, -1, 1, 0, 1, -1, 1, -1 }, dy[8] = { 1, 0, 0, -1, -1, -1, 1, 1 }; struct RollingHash { const _ulong MASK30 = (1ULL << 30) - 1; const _ulong MASK31 = (1ULL << 31) - 1; const _ulong MOD = (1ULL << 61) - 1; const _ulong MASK61 = MOD; vector<_ulong> hashed, power; RollingHash(const string& s, _ulong base) { int sz = SZ(s); hashed.assign(sz + 1, 0); power.assign(sz + 1, 0); power[0] = 1; for (int i = 0; i < sz; i++) { power[i + 1] = mul(power[i], base); hashed[i + 1] = mul(hashed[i], base) + s[i]; if (hashed[i + 1] >= MOD) hashed[i + 1] -= MOD; } } //乱数生成、main関数にコピペする void generate_base() { _ulong base; { //乱数生成 random_device seed_gen; mt19937 engine(seed_gen()); uniform_real_distribution<> dist1(129, INT_MAX); base = dist1(engine); } } _ulong get(int l, int r) const { _ulong ret = hashed[r] + MOD - mul(hashed[l], power[r - l]); if (ret >= MOD) ret -= MOD; return ret; } //a*b mod 2^61-1を返す関数 inline _ulong mul(_ulong a, _ulong b) const { _ulong u_a = a >> 31;//aの31bit以降 _ulong d_a = a & MASK31;//aの31bit未満 _ulong u_b = b >> 31; _ulong d_b = b & MASK31; _ulong mid = d_a * u_b + u_a * d_b; _ulong u_mid = mid >> 30; _ulong d_mid = mid & MASK30; return CalcMod(u_a * u_b * 2 + u_mid + (d_mid << 31) + d_a * d_b); } //mod 2^61-1を返す関数 inline _ulong CalcMod(_ulong x) const { _ulong u_x = x >> 61; _ulong d_x = x & MASK61; _ulong res = u_x + d_x; if (res >= MOD) res -= MOD; return res; } }; string s, t; lint M; int main() { _ulong base; { //乱数生成 random_device seed_gen; mt19937 engine(seed_gen()); uniform_real_distribution<> dist1(129, INT_MAX); base = dist1(engine); } cin >> s; cin >> M; RollingHash rh(s, base); lint ans = 0; REP(i, M) { cin >> t; RollingHash rh2(t, base); for (int i = 0; i + SZ(t) <= SZ(s); i++) { if (rh.get(i, i + SZ(t)) == rh2.get(0, SZ(t))) { ans++; } } } cout << ans << endk; }