#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") using MatchedPair = vector>; struct AhoCorasick { char e; vector Ps; vector V; // V[i]: 状態iと対応するパターン群 (iに到達=パターンiが検出) vecl failure; // failure[i]: 検索失敗時の状態iの遷移先 vector g; // g[i][c]: 状態iから遷移c+'a'(c+e)した時の遷移先 AhoCorasick(vector patterns, char e = 'a') : Ps(patterns), e(e) { // edgeに文字, nodeに状態 build(); // Trie木を作成 make_failure(); // BFSでfailureを埋めていく } void build() { g.emplace_back(vecl(26,0)); // g[0][i] = 0 V.emplace_back(vecl()); rep(i,sz(Ps)) { ll v = 0; for (auto _c : Ps[i]) { int c = _c - e; if (v > 0 && g[v][c] != -1) { v = g[v][c]; continue; } else if (v == 0 && g[v][c] != 0) { v = g[v][c]; continue; } int vs = sz(V); V.emplace_back(vecl()); g.emplace_back(vecl(26,-1)); g[v][c] = vs; v = vs; } assert(v < sz(V)); V[v].emplace_back(i); } } void make_failure() { failure.assign(sz(V)+1,0LL); queue S; S.push(0); while (!S.empty()) { ll current = S.front(); S.pop(); rep(i,26) { ll next = g[current][i]; if (next <= 0) continue; S.push(next); if (current != 0) { // 最長の接尾辞をfailureに ll f = failure[current]; while (g[f][i] == -1) f = failure[f]; failure[next] = g[f][i]; // fからi+'a'の遷移ができる状態fを探して,その遷移先g[f][i]をnextのfailureとする for (auto p : V[failure[next]]) V[next].emplace_back(p); } } } } void search(const string &S, MatchedPair &X) { // マッチしたものはXに格納 ll v = 0; rep(i,sz(S)) { int c = S[i] - e; while (g[v][c] == -1) v = failure[v]; v = g[v][c]; rep(j,sz(V[v])) { string x = Ps[V[v][j]]; X.emplace_back(x,pairl{i-sz(x)+1,i}); } } } void print() { rep(i,sz(V)) { rep(j,26) { int p = i == 0 ? 0 : -1; if (g[i][j] == p) continue; printf("%d -> %d (%c)\n",i,g[i][j],j+e); } } dump(failure); } }; int solve(ostringstream &cout) { #ifdef LOCAL ifstream in("../../Atcoder/input.txt"); cin.rdbuf(in.rdbuf()); #endif string S; cin>>S; ll M; cin>>M; vector Ps(M); rep(q,M) cin>>Ps[q]; MatchedPair res; AhoCorasick ac(Ps,'A'); ac.search(S,res); cout << sz(res) << endl; return 0; } int main() { ostringstream oss; int res = solve(oss); cout << oss.str(); return res; }