//include //------------------------------------------ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}} using LL = long long; //------------------------------------------ //------------------------------------------ struct RollingHash { using long_type = unsigned; using hash_type = tuple; private: int s_len; long_type mod1; long_type mod2; long_type mod3; long_type base1; long_type base2; long_type base3; vector hash1; vector hash2; vector hash3; vector pow1; vector pow2; vector pow3; public: RollingHash() : base1(1009), base2(1007), base3(2339), mod1(1000000007), mod2(1000000009), mod3(1000000087) { } RollingHash(const string s) : RollingHash() { init(s); } void init(const string s) { const int n = s.size(); s_len = n; hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); hash3.assign(n + 1, 0); pow1.assign(n + 1, 1); pow2.assign(n + 1, 1); pow3.assign(n + 1, 1); for (int i = 0; i < n; i++) { hash1[i + 1] = ((hash1[i] + s[i]) * base1); hash2[i + 1] = ((hash2[i] + s[i]) * base2); hash3[i + 1] = ((hash3[i] + s[i]) * base3); pow1[i + 1] = (pow1[i] * base1); pow2[i + 1] = (pow2[i] * base2); pow3[i + 1] = (pow3[i] * base3); } } //0-index //s[l, r)のハッシュタプルを返す hash_type get_hash_tuple(int l, int r) { long_type h1 = ((hash1[r] - hash1[l] * pow1[r - l] + mod1) % mod1 + mod1) % mod1; long_type h2 = ((hash2[r] - hash2[l] * pow2[r - l] + mod2) % mod2 + mod2) % mod2; long_type h3 = ((hash3[r] - hash3[l] * pow3[r - l] + mod3) % mod3 + mod3) % mod3; return make_tuple(h1, h2, h3); } //not verify //htype_1のハッシュの後ろに長さh2_lenのハッシュを結合する hash_type concat(hash_type h_tup1, hash_type h_tup2, int h2_len) { long_type h1 = (get<0>(h_tup1) * pow1[h2_len] + get<0>(h_tup2)) % mod1; long_type h2 = (get<1>(h_tup1) * pow2[h2_len] + get<1>(h_tup2)) % mod2; long_type h3 = (get<2>(h_tup1) * pow3[h2_len] + get<2>(h_tup2)) % mod3; return make_tuple(h1, h2, h3); } //先頭i文字のハッシュタプルを返す hash_type get_from_head(int len) { return get_hash_tuple(0, len); } //末尾i文字のハッシュタプルを返す hash_type get_from_tail(int len) { return get_hash_tuple(s_len - len, s_len); } int contain(string s) { int ret = 0; int m_len = this->s_len; int o_len = s.size(); RollingHash o_hash(s); for (int i = 0; i < m_len - o_len + 1; i++) { auto h1 = get_hash_tuple(i, i + o_len); auto h2 = o_hash.get_hash_tuple(0, o_len); if (RollingHash::same(h1, h2)) ret++; } return ret; } static bool same(hash_type h_tup1, hash_type h_tup2) { return h_tup1 == h_tup2; } }; int main() { string S; cin >> S; RollingHash roll(S); int N; cin >> N; LL ans = 0; for (int i = 0; i < N; i++) { string s; cin >> s; ans += roll.contain(s); } cout << ans << endl; }