#include // #include using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define pb push_back #define m0(x) memset(x,0,sizeof(x)) #define fill(x,y) memset(x,y,sizeof(x)) #define bg begin() #define ed end() #define all(x) x.bg,x.ed //#define mp make_pair #define vi vector #define vvi vector #define vll vector #define vvll vector #define vs vector #define vvs vector #define vc vector #define vvc vector #define pii pair #define pllll pair #define vpii vector> #define vpllll vector> #define vpis vector> #define vplls vector> #define vpsi vector> #define vpsll vector> template void chmax(T &a, const T &b) {a = (a > b? a : b);} template void chmin(T &a, const T &b) {a = (a < b? a : b);} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits::max() / 10; const ld pi = acos(-1); const ll mod = 998244353; int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[] = {0, -1, 0, 1, -1, 1, -1, 1}; #define int long long #define uint uint64_t struct Rolling_Hash { static const uint64_t rh_mod = (1ull << 61ull) - 1; using uint128_t = __uint128_t; vector power; uint64_t base; static inline uint64_t add(uint64_t a, uint64_t b) { if((a += b) >= rh_mod) a -= rh_mod; return a; } static inline uint64_t mul(uint64_t a, uint64_t b) { uint128_t c = (uint128_t) a * b; return add(c >> 61, c & rh_mod); } static inline uint64_t generate_base() { mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution< uint64_t > rand(1, Rolling_Hash::rh_mod - 1); return rand(mt); } inline void expand(size_t sz) { if((int)power.size() < (int)sz + 1) { int pre_sz = (int) power.size(); power.resize(sz + 1); for(int i = pre_sz - 1; i < (int)sz; i++) { power[i + 1] = mul(power[i], base); } } } Rolling_Hash() { base = generate_base(); power = {1}; } vector build(const string &s) const { int sz = s.size(); vector hashed(sz + 1); for(int i = 0; i < (int)sz; i++) { hashed[i + 1] = add(mul(hashed[i], base), s[i]); } return hashed; } template vector build(const vector &s) const { int sz = s.size(); vector hashed(sz + 1); for(int i = 0; i < (int)sz; i++) { hashed[i + 1] = add(mul(hashed[i], base), s[i]); } return hashed; } vector build_string(const string &s) const { vector ss; for(char c : s) ss.push_back(c); return build(ss); } uint64_t query(const vector &s, int l, int r) { expand(r - l); return add(s[r], rh_mod - mul(s[l], power[r - l])); } uint64_t combine(uint64_t h1, uint64_t h2, size_t h2len) { expand(h2len); return add(mul(h1, power[h2len]), h2); } int lcp(const vector &a, int l1, int r1, const vector &b, int l2, int r2) { int len = min(r1 - l1, r2 - l2); int low = 0, high = len + 1; while(high - low > 1) { int mid = (low + high) / 2; if(query(a, l1, l1 + mid) == query(b, l2, l2 + mid)) low = mid; else high = mid; } return low; } }; void solve() { //euler_tourして区間の数え上げになる //区間上で値がkであるような数。 //static range count query //indexを保持して二分探索 //range sum query string s; cin >> s; Rolling_Hash rh; int n = s.size(); vector hash = rh.build_string(s); int m; cin >> m; int ans = 0; while(m--) { string c; cin >> c; int sz = (int)c.size(); vector hash2 = rh.build_string(c); FOR(n-sz+1) { if(rh.lcp(hash, i, i+sz, hash2, 0, sz) == sz) ans++; } } cout << ans << endl; } //abc102 Equal Cut signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }