結果

問題 No.430 文字列検索
ユーザー firiexpfiriexp
提出日時 2019-11-21 22:19:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,487 bytes
コンパイル時間 1,271 ms
コンパイル使用メモリ 111,888 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2024-04-19 10:05:24
合計ジャッジ時間 2,177 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 155 ms
27,392 KB
testcase_02 AC 9 ms
6,940 KB
testcase_03 AC 9 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 141 ms
26,880 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 11 ms
6,940 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 47 ms
8,320 KB
testcase_14 AC 46 ms
6,940 KB
testcase_15 WA -
testcase_16 AC 14 ms
6,944 KB
testcase_17 AC 10 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <chrono>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;

using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

using u64 = unsigned long long;

constexpr u64 M = (1UL << 61) - 1;
constexpr u64 POSITIVISER = M * 3;
constexpr u64 MASK30 = (1UL << 30) - 1;
constexpr u64 MASK31 = (1UL << 31) - 1;



class rolling_hash_u64 {

    static u64 get_base(){
        u64 z = (static_cast<uint64_t>((chrono::system_clock::now().time_since_epoch().count())&((1LL << 32)-1)))+0x9e3779b97f4a7c15;
        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
        z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
        return z;
    }

    static u64 &B() {
        static u64 B_ = (get_base())%(M-2)+2;
        return B_;
    }
    static vector<u64> &p() {
        static vector<u64> p_{1, B()};
        return p_;
    }

    static inline u64 mul(u64 x, u64 y){
        u64 a = x >> 31, b = x & MASK31, c = y >> 31, d = y & MASK31;
        u64 e = b*c+a*d, f = e >> 30, g = e & MASK30;
        return a*c*2 + f + (g << 31) + b*d;
    }

    static inline u64 calc_mod(u64 val){
        val = (val & M) + (val >> 61);
        if(val > M) val -= M;
        return val;
    }
public:
    vector<u64> hash;
    explicit rolling_hash_u64(const string &s) {
        if(p().size() <= s.size()){
            int l = p().size();
            p().resize(s.size()+1);
            for (int i = l; i < p().size(); ++i) {
                p()[i] = calc_mod(mul(p()[i-1], p()[1]));
            }
        }
        hash.resize(s.size()+1, 0);
        for (int i = 0; i < s.size(); ++i) {
            hash[i+1] = calc_mod(mul(hash[i],B()) + s[i]);
        }
    };

    u64 get(int l, int r){
        return calc_mod(hash[r] + POSITIVISER - mul(hash[l], p()[r-l]));
    }
};

int main() {
    string s;
    cin >> s;
    int n = s.size();
    vector<map<u64, int>> v(11);
    rolling_hash_u64 sh(s);
    for (int i = 1; i <= 10; ++i) {
        for (int j = 0; j+i <= n; ++j) {
            v[i][sh.get(j, j+i)]++;
        }
    }
    int m;
    cin >> m;
    ll ans = 0;
    for (int i = 0; i < m; ++i) {
        string t;
        cin >> t;
        rolling_hash_u64 th(t);
        ans += v[t.size()][th.get(0, t.size())];
    }
    cout << ans << "\n";
    return 0;
}
0