結果

問題 No.430 文字列検索
ユーザー 👑 tatyamtatyam
提出日時 2019-09-12 23:15:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,540 bytes
コンパイル時間 4,178 ms
コンパイル使用メモリ 217,048 KB
実行使用メモリ 8,628 KB
最終ジャッジ日時 2023-09-15 14:24:31
合計ジャッジ時間 9,755 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned;
using ull = unsigned long long;


constexpr ull mod = 0x1fffffffffffffff, base = 257;
constexpr ull mask(int a) { return (1ull << a) - 1; }
inline constexpr ull mul(ull a, ull b) {
    __uint128_t ans = __uint128_t(a) * b;
    ans = (ans >> 61) + (ans & mask(61));
    if(ans >= mod) ans -= mod;
    return ans;
}
struct RollingHash {
    ull hashed[50001], power[50001];
    RollingHash(const string &s) {
        power[0] = 1;
        for(uint i = 0; i < s.size(); i++) {
            power[i + 1] = mul(power[i], base);
            hashed[i + 1] = mul(hashed[i], base) + s[i];
            if(hashed[i + 1] >= mod) hashed[i + 1] -= mod;
        }
    }
    ull get(uint l, uint r) const {
        ull ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
        if(ret >= mod) ret -= mod;
        return ret;
    }
};
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    int m;
    cin >> s >> m;
    RollingHash rh(s);
    int ans = 0;
    for(uint i = 0; i < m; i++) {
        string t;
        cin >> t;
        __uint128_t rh2 = 0;
        for(auto i : t) {
            rh2 *= base;
            rh2 = (rh2 >> 61) + (rh2 & mask(61));
            rh2 += i;
            if(rh2 >= mod) rh2 -= mod;
        }
        for(uint i = t.size(); i < s.size() + 1; i++) ans += rh.get(i - t.size(), i) == rh2;
    }
    cout << ans << endl;
}

0