結果

問題 No.430 文字列検索
ユーザー fumuumuffumuumuf
提出日時 2022-05-10 17:55:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 901 ms / 2,000 ms
コード長 9,710 bytes
コンパイル時間 2,246 ms
コンパイル使用メモリ 185,316 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-10 01:00:25
合計ジャッジ時間 11,373 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 891 ms
5,248 KB
testcase_02 AC 895 ms
5,248 KB
testcase_03 AC 901 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 5 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 4 ms
5,248 KB
testcase_11 AC 895 ms
5,248 KB
testcase_12 AC 899 ms
5,248 KB
testcase_13 AC 899 ms
5,248 KB
testcase_14 AC 894 ms
5,248 KB
testcase_15 AC 893 ms
5,248 KB
testcase_16 AC 893 ms
5,248 KB
testcase_17 AC 896 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<ll, ll>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VP = vector<P>;
using VVP = vector<vector<P>>;
using VS = vector<string>;
using VC = vector<char>;
using VVC = vector<vector<char>>;
using VD = vector<double>;
using VVD = vector<vector<double>>;
using I_I = pair<int, int>;
// using VI_I = vector<I_I>;
// using VVI_I = vector<vector<I_I>>;
// #define MOD 1000000007
#define MOD 998244353
const int INF = (int) 1e9 + 10;         // int max > 2*10^9
const long long INFL = (ll) 2e18 + 10;  // ll max > 9*10^18

P operator+(const P &a, const P &b) { return make_pair(a.first + b.first, a.second + b.second); }

P operator-(const P &a, const P &b) { return make_pair(a.first - b.first, a.second - b.second); }

P operator*(const P &a, const P &b) { return make_pair(a.first * b.first, a.second * b.second); }

P operator/(const P &a, const P &b) { return make_pair(a.first / b.first, a.second / b.second); }

#define pmod(x, y) ((x%y)+y)%y

// jhelper 用変数
ostream *__cout;
istream *__cin;

/********** libs **********/
// 最大公約数, 最小公倍数
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <class T, class C>
bool chmax(T& a, C b) { if (a < b) { a = b; return true; } return false; }
template <class T, class C>
bool chmin(T& a, C b) { if (a > b) { a = b; return true; } return false; }

template <class T>
T sum(const vector<T>& v) {
    T res = 0;
    for (size_t i = 0; i < v.size(); ++i) res += v[i];
    return res;
}

template <class T> VP idx_pair(T vals) { VP ans; rep(i, vals.size()) { ans.emplace_back(vals[i], i); } return ans; }
void _init_io(std::istream &cin, std::ostream &cout) {
    // 入出力高速化用
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // 少数15桁まで出力
    cout << std::setprecision(15);
    ///////////////////////////////
    __cout = &cout;// jhelper 用
    __cin = &cin; // jhelper 用
}

/* デバッグ用 出力 */
template <typename T> ostream& operator<<(ostream& os, const deque<T>& vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template <typename T> ostream& operator<<(ostream& os, const set<T>& vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream& operator<<(ostream& os, const unordered_multiset<T>& vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; }
template <typename TK, typename TV> ostream& operator<<(ostream& os, const map<TK, TV>& mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template <typename TK, typename TV> ostream& operator<<(ostream& os, const unordered_map<TK, TV>& mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
/////////////////////////////////////////////////////////
// print like python
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////


void print() { *__cout << endl; }

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    *__cout << head;
    if (sizeof...(tail) != 0) *__cout << " ";
    print(forward<Tail>(tail)...);
}

template <class T>
void print(vector<T>& vec) {
    for (auto& a : vec) {
        *__cout << a;
        if (&a != &vec.back()) *__cout << " ";
    }
    *__cout << endl;
}

template <class T>
void print(vector<vector<T>>& df) {
    for (auto& vec : df) {
        print(vec);
    }
}

/*
 * 以下の vector 入力パクった.
 * jhelper で使用するため, 全部 cin での入力にしている. (現状のレベルでは困らないので)
 *
 * https://atcoder.jp/contests/abc213/submissions/24901856
 */


template<class T> void scan(T& a){ *__cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto&& i : a) scan(i); }
void in(){}
template <class... T> void in(T&... a){ (void)initializer_list<int>{ (scan(a), 0)... }; }

//////////////////////////////////////////////////
// vector 定義
#define let_v(type,name,...) vector<type>name(__VA_ARGS__)
// vector 定義 + 入力
#define let_V(type,name,size) vector<type>name(size);in(name)
// 2次元 vector 定義
#define let_vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
// 2次元 vector 定義 + 入力
#define let_VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define let_vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
//////////////////////////////////////////////////

/* string 入力を vector<char> で受け取るみたいな */
vector<char> let_CharVec() { string s; *__cin >> s; return {s.begin(), s.end()}; }

// add other source

// よく使う
// #include "../library/graph/UnionFind.cpp"
// #include "../library/tree/SegmentTree.cpp"
// #include "../library/graph/template.cpp"


// utils
// #include "../library/mint.cpp"
// #include "../library/string/utils.cpp"
// #include "../library/math/utils.cpp"
/**************************/

/**
 * src: https://ei1333.github.io/library/string/rolling-hash.cpp.html
 *
 * @brief Rolling-Hash(ローリングハッシュ)
 * @see https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
 * @docs docs/rolling-hash.md
 */
struct RollingHash {
    static const uint64_t mod = (1ull << 61ull) - 1;
    using uint128_t = __uint128_t;
    vector<uint64_t> power;
    const uint64_t base;

    static inline uint64_t add(uint64_t a, uint64_t b) {
        if ((a += b) >= mod) a -= 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 & 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, RollingHash::mod - 1);
        return rand(mt);
    }

    inline void expand(size_t sz) {
        if (power.size() < sz + 1) {
            int pre_sz = (int) power.size();
            power.resize(sz + 1);
            for (int i = pre_sz - 1; i < sz; i++) {
                power[i + 1] = mul(power[i], base);
            }
        }
    }

    explicit RollingHash(uint64_t base = generate_base()) : base(base), power{1} {}

    vector<uint64_t> build(const string &s) const {
        int sz = s.size();
        vector<uint64_t> hashed(sz + 1);
        for (int i = 0; i < sz; i++) {
            hashed[i + 1] = add(mul(hashed[i], base), s[i]);
        }
        return hashed;
    }

    template<typename T>
    vector<uint64_t> build(const vector<T> &s) const {
        int sz = s.size();
        vector<uint64_t> hashed(sz + 1);
        for (int i = 0; i < sz; i++) {
            hashed[i + 1] = add(mul(hashed[i], base), s[i]);
        }
        return hashed;
    }

    uint64_t query(const vector<uint64_t> &s, int l, int r) {
        expand(r - l);
        return add(s[r], 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<uint64_t> &a, int l1, int r1, const vector<uint64_t> &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;
    }
};

class yukiko {
public:
//@formatter:off
void solve(std::istream &cin, std::ostream &cout) { _init_io(cin, cout); int t = 1;
    while (t--) _solve(cin, cout); }
//@formatter:on

    int _solve(std::istream &cin, std::ostream &cout) {
        // write here
        string s;
        cin >> s;
        RollingHash rh;
        auto hash_s = rh.build(s);
        int m;
        cin >> m;
        ll cnt=0;
        rep(i, m) {
            string t;
            cin >> t;
            auto hash_t = rh.build(t);
            auto v=rh.query(hash_t,0,t.size());
            rep(j,s.size()){
                if(j+t.size()>s.size())break;
                auto u=rh.query(hash_s,j,j+t.size());
                if(u==v)cnt++;
            }
        }
        print(cnt);

//        rh.build()


        return 0;
    }

};




int main() {
	yukiko solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}
0