#include using namespace std; using ll = long long; using ull = unsigned long long; using i128 = __int128_t; using pii = pair; using pll = pair; template using vec = vector; template using vvec = vector>; #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) constexpr char ln = '\n'; istream& operator>>(istream& is, __int128_t& x) { x = 0; string s; is >> s; int n = int(s.size()), it = 0; if (s[0] == '-') it++; for (; it < n; it++) x = (x * 10 + s[it] - '0'); if (s[0] == '-') x = -x; return is; } ostream& operator<<(ostream& os, __int128_t x) { if (x == 0) return os << 0; if (x < 0) os << '-', x = -x; deque deq; while (x) deq.emplace_front(x % 10), x /= 10; for (int e : deq) os << e; return os; } template ostream& operator<<(ostream& os, const pair& p) { return os << "(" << p.first << ", " << p.second << ")"; } template ostream& operator<<(ostream& os, const vector& v) { os << "{"; for (int i = 0; i < int(v.size()); i++) { if (i) os << ", "; os << v[i]; } return os << "}"; } template inline int SZ(const Container& v) { return int(v.size()); } template inline void UNIQUE(vector& v) { v.erase(unique(v.begin(), v.end()), v.end()); } template inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return true; } return false; } inline int topbit(ull x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); } inline int botbit(ull x) { return x == 0 ? 64 : __builtin_ctzll(x); } inline int popcount(ull x) { return __builtin_popcountll(x); } inline int kthbit(ull x, int k) { return (x >> k) & 1; } inline constexpr long long TEN(int x) { return x == 0 ? 1 : TEN(x-1) * 10; } template inline T ABS(T x) { return max(x, -x); } const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; inline void YES(bool t = 1) { cout << YESNO[t] << "\n"; } inline void Yes(bool t = 1) { cout << YesNo[t] << "\n"; } inline void yes(bool t = 1) { cout << yesno[t] << "\n"; } inline void print() { cout << "\n"; } template inline void print(const vector& v) { for (auto it = begin(v); it != end(v); ++it) { if (it != begin(v)) cout << " "; cout << *it; } print(); } template inline void print(const T& x, const Args& ... args) { cout << x << " "; print(args...); } #ifdef MINATO_LOCAL inline void debug_out() { cerr << endl; } template inline void debug_out(const T& x, const Args& ... args) { cerr << " " << x; debug_out(args...); } #define debug(...) cerr << __LINE__ << " : [" << #__VA_ARGS__ << "] =", debug_out(__VA_ARGS__) #else #define debug(...) (void(0)) #endif struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(7); }; } fast_ios_; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template struct SuffixArray { public: SuffixArray() {} SuffixArray(const string& s) : n_(int(s.size())), data(s.size()), invs(s.size()) { vector s2(n_); for (int i = 0; i < n_; i++) { data[i] = s2[i] = s[i]; } sa_ = sa_is(s2,255); for (int i = 0; i < n_; i++) { invs[sa_[i]] = i; } } SuffixArray(const vector& s) : n_(int(s.size())), data(s), invs(s.size()) { vector idx(n_); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&](int l, int r) { return s[l] < s[r]; }); vector s2(n_); int now = 0; for (int i = 0; i < n_; i++) { if (i && s[idx[i - 1]] != s[idx[i]]) now++; s2[idx[i]] = now; } sa_ = sa_is(s2, now); for (int i = 0; i < n_; i++) { invs[sa_[i]] = i; } } SuffixArray(const vector& s, int upper) : n_(int(s.size())), data(s.size()), invs(s.size()) { assert(0 <= upper); for (int i = 0; i < n_; i++) { assert(0 <= s[i] && s[i] <= upper); data[i] = s[i]; } auto sa_ = sa_is(s, upper); for (int i = 0; i < n_; i++) { invs[sa_[i]] = i; } } int size() const { return n_; } int operator[](int i) const { assert(0 <= i and i < n_); return sa_[i]; } int inv(int i) const { assert(0 <= i and i < n_); return invs[i]; } T dat(int i) const { assert(0 <= i and i < n_); return data[i]; } int lower_bound(const vector& t) const { int l = -1, r = n_; while (r - l > 1) { int m = (l + r) / 2; (lt_substr(t, sa_[m]) ? l : r) = m; } return r; } int lower_bound(const string& t) const { vector t2 = stovi(t); int r = lower_bound(t2); return r; } int upper_bound(vector t) const { if (t.empty()) return 0; t.back()++; int r = lower_bound(t); return r; } int upper_bound(const string& t) const { vector t2 = stovi(t); int r = upper_bound(t2); return r; } int count(const vector& t) const { return upper_bound(t) - lower_bound(t); } // O(|T|*log|S|) int count(const string& t) const { vector t2 = stovi(t); return count(t2); } private: int n_; vector data; vector sa_,invs; vector stovi(const string& s) const { int n = int(s.size()); vector ret(n); for (int i = 0; i < n; i++) { ret[i] = s[i]; } return ret; } bool lt_substr(const vector& t, int pos_s) const { int m = int(t.size()); int pos_t = 0; while(pos_s < n_ and pos_t < m) { if(data[pos_s] < t[pos_t]) return true; if(data[pos_s] > t[pos_t]) return false; pos_s++; pos_t++; } return pos_s == n_ and pos_t < m; } vector sa_naive(const vector& s) const { int n = int(s.size()); vector sa(n); iota(sa.begin(), sa.end(), 0); sort(sa.begin(), sa.end(), [&](int l, int r) { if (l == r) return false; while (l < n && r < n) { if (s[l] != s[r]) return s[l] < s[r]; l++; r++; } return l == n; }); return sa; } vector sa_doubling(const vector& s) const { int n = int(s.size()); vector sa(n), rnk = s, tmp(n); iota(sa.begin(), sa.end(), 0); for (int k = 1; k < n; k *= 2) { auto cmp = [&](int x, int y) { if (rnk[x] != rnk[y]) return rnk[x] < rnk[y]; int rx = x + k < n ? rnk[x + k] : -1; int ry = y + k < n ? rnk[y + k] : -1; return rx < ry; }; sort(sa.begin(), sa.end(), cmp); tmp[sa[0]] = 0; for (int i = 1; i < n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0); } swap(tmp, rnk); } return sa; } // SA-IS, linear-time suffix array construction // Reference: // G. Nong, S. Zhang, and W. H. Chan, // Two Efficient Algorithms for Linear Time Suffix Array Construction template vector sa_is(const vector& s, int upper) const { int n = int(s.size()); if (n == 0) return {}; if (n == 1) return {0}; if (n == 2) { if (s[0] < s[1]) { return {0, 1}; } else { return {1, 0}; } } if (n < THRESHOLD_NAIVE) { return sa_naive(s); } if (n < THRESHOLD_DOUBLING) { return sa_doubling(s); } vector sa(n); vector ls(n); for (int i = n - 2; i >= 0; i--) { ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]); } vector sum_l(upper + 1), sum_s(upper + 1); for (int i = 0; i < n; i++) { if (!ls[i]) { sum_s[s[i]]++; } else { sum_l[s[i] + 1]++; } } for (int i = 0; i <= upper; i++) { sum_s[i] += sum_l[i]; if (i < upper) sum_l[i + 1] += sum_s[i]; } auto induce = [&](const vector& lms) { fill(sa.begin(), sa.end(), -1); vector buf(upper + 1); copy(sum_s.begin(), sum_s.end(), buf.begin()); for (auto d : lms) { if (d == n) continue; sa[buf[s[d]]++] = d; } copy(sum_l.begin(), sum_l.end(), buf.begin()); sa[buf[s[n - 1]]++] = n - 1; for (int i = 0; i < n; i++) { int v = sa[i]; if (v >= 1 && !ls[v - 1]) { sa[buf[s[v - 1]]++] = v - 1; } } copy(sum_l.begin(), sum_l.end(), buf.begin()); for (int i = n - 1; i >= 0; i--) { int v = sa[i]; if (v >= 1 && ls[v - 1]) { sa[--buf[s[v - 1] + 1]] = v - 1; } } }; vector lms_map(n + 1, -1); int m = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms_map[i] = m++; } } vector lms; lms.reserve(m); for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms.push_back(i); } } induce(lms); if (m) { vector sorted_lms; sorted_lms.reserve(m); for (int v : sa) { if (lms_map[v] != -1) sorted_lms.push_back(v); } vector rec_s(m); int rec_upper = 0; rec_s[lms_map[sorted_lms[0]]] = 0; for (int i = 1; i < m; i++) { int l = sorted_lms[i - 1], r = sorted_lms[i]; int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n; int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n; bool same = true; if (end_l - l != end_r - r) { same = false; } else { while (l < end_l) { if (s[l] != s[r]) { break; } l++; r++; } if (l == n || s[l] != s[r]) same = false; } if (!same) rec_upper++; rec_s[lms_map[sorted_lms[i]]] = rec_upper; } auto rec_sa = sa_is(rec_s, rec_upper); for (int i = 0; i < m; i++) { sorted_lms[i] = lms[rec_sa[i]]; } induce(sorted_lms); } return sa; } }; int main() { string S; cin >> S; SuffixArray SA(S); ll ans = 0; int M; cin >> M; rep(i,M) { string T; cin >> T; ans += SA.count(T); } cout << ans << ln; }