結果

問題 No.430 文字列検索
ユーザー 👑 tatyamtatyam
提出日時 2019-09-13 00:05:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,889 ms / 2,000 ms
コード長 4,520 bytes
コンパイル時間 1,427 ms
コンパイル使用メモリ 148,704 KB
実行使用メモリ 18,512 KB
最終ジャッジ日時 2023-09-15 14:30:54
合計ジャッジ時間 20,097 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1,889 ms
18,500 KB
testcase_02 AC 1,760 ms
18,400 KB
testcase_03 AC 1,734 ms
18,448 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 31 ms
18,488 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 6 ms
4,796 KB
testcase_11 AC 1,756 ms
18,424 KB
testcase_12 AC 1,721 ms
18,400 KB
testcase_13 AC 1,752 ms
18,500 KB
testcase_14 AC 1,779 ms
18,436 KB
testcase_15 AC 1,711 ms
18,508 KB
testcase_16 AC 1,709 ms
18,512 KB
testcase_17 AC 1,753 ms
18,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>

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;

//------------------------------------------
//------------------------------------------



using long_type = unsigned long long;
using hash_type = tuple<long_type, long_type, long_type>;
hash_type mem[50000][11];
struct RollingHash {
    
    
protected:
    
    int s_len;
    
    long_type mod1;
    long_type mod2;
    long_type mod3;
    
    long_type base1;
    long_type base2;
    long_type base3;
    
    vector<long_type> hash1;
    vector<long_type> hash2;
    vector<long_type> hash3;
    
    vector<long_type> pow1;
    vector<long_type> pow2;
    vector<long_type> pow3;
    
public:
    
    RollingHash() :
    base1(1009LL), base2(1007LL), base3(2339LL),
    mod1(1000000007LL), mod2(1000000009LL), mod3(1000000087LL) {
    }
    
    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] * base1 + s[i]) % mod1;
            hash2[i + 1] = (hash2[i] * base2 + s[i]) % mod2;
            hash3[i + 1] = (hash3[i] * base3 + s[i]) % mod3;
            pow1[i + 1] = pow1[i] * base1 % mod1;
            pow2[i + 1] = pow2[i] * base2 % mod2;
            pow3[i + 1] = pow3[i] * base3 % mod3;
        }
    }
    
    //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;
        long_type h2 = (hash2[r] - hash2[l] * pow2[r - l] % mod2 + mod2) % mod2;
        long_type h3 = (hash3[r] - hash3[l] * pow3[r - l] % 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);
        auto h2 = o_hash.get_hash_tuple(0, o_len);
        for (int i = 0; i < m_len - o_len + 1; i++) {
            auto h1 = mem[i][o_len] == hash_type() ? mem[i][o_len] = get_hash_tuple(i, i + o_len) : mem[i][o_len];
            if (RollingHash::same(h1, h2)) ret++;
        }
        return ret;
    }
    
    int get_length() {
        return s_len;
    }
    
    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;
    
}




0