#include using namespace std; // #define LOCAL // 提出時はコメントアウト #define DEBUG_ typedef long long ll; const double EPS = 1e-9; const ll INF = ((1LL<<62)-(1LL<<31)); typedef vector vecl; typedef pair pairl; template using uset = unordered_set; template using mapv = map>; template using umap = unordered_map; #define ALL(v) v.begin(), v.end() #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define sz(x) (ll)x.size() ll llceil(ll a,ll b) { return (a+b-1)/b; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template vector> genarr(ll n, ll m, T init) { return vector>(n,vector(m,init)); } ///// DEBUG #define DUMPOUT cerr #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) templateistream&operator>>(istream&is,vector&vec){for(T&x:vec)is>>x;return is;} templateostream&operator<<(ostream&os,pair&pair_var){os<<"("<ostream&operator<<(ostream&os,const vector&vec){os<<"{";for(int i=0;iostream&operator<<(ostream&os,map&map_var){os<<"{";repi(itr,map_var){os<<*itr;itr++;if(itr!=map_var.end())os<<", ";itr--;} os<<"}";return os;} templateostream&operator<<(ostream&os,set&set_var){os<<"{";repi(itr,set_var){os<<*itr;itr++;if(itr!=set_var.end())os<<", ";itr--;} os<<"}";return os;} void dump_func(){DUMPOUT<void dump_func(Head&&head,Tail&&...tail){DUMPOUT<0){DUMPOUT<<", ";} dump_func(std::move(tail)...);} #ifndef LOCAL #undef DEBUG_ #endif #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \ << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif ////////// #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") int solve(ostringstream &cout) { #ifdef LOCAL ifstream in("../../Atcoder/input.txt"); cin.rdbuf(in.rdbuf()); #endif string S; cin>>S; ll M; cin>>M; ll ans = 0; rep(q,M) { string P; cin>>P; ll sub = 0; vecl table(sz(P)+1,0LL); // table[i]: iでfailした時, どこから検索し直すか table[0] = -1; for (int i = 0, p = -1; i < sz(P); i++) { while (p >= 0 && P[i] != P[p]) p = table[p]; // P[i]と重複している先頭部分P[0:p]を探す table[i+1] = ++p; // P[0:p]が一致しているので, i+1でfailした場合, 検索し直す先頭はP[p+1]から } for (int i = 0, p = 0; i < sz(S); i++) { while (p >= 0 && S[i] != P[p]) p = table[p]; if (p == sz(P) - 1) sub++; p++; } ans += sub; dump(sub); } cout << ans << endl; return 0; } int main() { ostringstream oss; int res = solve(oss); cout << oss.str(); return res; }