結果

問題 No.430 文字列検索
ユーザー lightninglightning
提出日時 2020-03-19 18:53:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,868 bytes
コンパイル時間 1,631 ms
コンパイル使用メモリ 173,824 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-20 21:03:19
合計ジャッジ時間 26,781 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define IOS cin.tie(0),ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<ll, ll> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
template<class Type>struct is_vector : std::false_type {};
template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
    if (v.empty())return out;
    constexpr bool is_vector_v = is_vector<T>::value;
    if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
    else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
    return out;
}
inline void put() {}
template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
inline void putn() {}
template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

template <uint_fast64_t basesize>
class RollingHash {
public:
    using u64 = uint_fast64_t;
    using i32 = int_fast32_t;
    using arr = array<u64, basesize>;
    const u64 mod = (1ull << 61) - 1;
    const u64 m30 = (1ull << 30) - 1;
    const u64 m31 = (1ull << 31) - 1;
    const u64 p31 = (1ull << 31);
    arr base;
    RollingHash() {
        random_device seed_gen;
        mt19937_64 engine(seed_gen());
        for (u64 i = 0; i < basesize; ++i) {
            base[i] = (engine() & m30) + m30;
        }
    }
    u64 mul(u64 a, u64 b) {
        u64 au = a >> 31;
        u64 ad = a & m31;
        u64 bu = b >> 31;
        u64 bd = b & m31;
        u64 mid = ad * bu + au * bd;
        u64 midu = mid >> 30;
        u64 midd = mid & m30;
        u64 res = au * bu * 2 + midu + (midd << 31) + ad * bd;
        return res;
    }
    u64 calcMod(u64 v) {
        v = (v & mod) + (v >> 61);
        if (v > mod)v - mod;
        return v;
    }
    vector<arr> calcHash(string &s,i32 l,i32 r) { // s[l,r)のhashを計算
        if (l >= r || r > s.size() || l < 0) return vector<arr>(0);
        vector<arr> res(r - l);
        for (i32 j = 0; j < basesize ; ++j) {
            res[0][j] = s[l];
        }
        for (i32 i = 1; i < r - l; ++i) {
            for (i32 j = 0; j < basesize; ++j) {
                res[i][j] = calcMod(mul(res[i - 1][j], base[j]) + s[i + l]);
            }
        }
        return res;
    }
    vector<arr> calcHash(string &s) { // s[l,r)のhashを計算
        return calcHash(s,0,s.size());
    }
    vector<arr> calcHashLen(string& s, i32 len) {
        if (len > s.size()) return vector<arr>(0);
        i32 l = (i32)s.size() - len + 1;
        vector<arr> pre = calcHash(s, 0, len);
        vector<arr> res(l);
        const u64 pos = mod * 3;
        arr p;
        fill(p.begin(), p.end(), 1);
        if (0 < len) {
            for (i32 j = 0; j < basesize; ++j) {
                res[0][j] = pre[len - 1][j];
            }
        }
        for (i32 i = 0; i < len; ++i) {
            for (i32 j = 0; j < basesize; ++j) {
                p[j] = calcMod(mul(p[j], base[j]));
            }
        }
        for (i32 i = 1; i < l; ++i) {
            for (i32 j = 0; j < basesize; ++j) {
                res[i][j] = calcMod(mul(res[i - 1][j], base[j]) + s[i - 1 + len]);//添え字周りが怪しい
                res[i][j] = calcMod(res[i][j] + pos - mul(s[i - 1], p[j]));
            }
        }
        return res;
    }
};

constexpr uint_fast64_t  bs = 1;
using arr = array<uint_fast64_t, bs>;
using RH = RollingHash<bs>;

int main(){
    IOS;
    string s;
    cin >> s;
    int m;
    cin >> m;
    vector<string> c(m);
    REP(i,m){
        cin >> c[i];
    }
    RH rh;
    int res = 0;
    REP(i,m){
        auto ar = rh.calcHashLen(s, c[i].size());
        auto ha = rh.calcHash(c[i]);
        REP(j,ar.size()){
            if(ar[j]==ha.back()){
                res++;
            }
        }
    }
    put(res);
    return 0;
}
0