結果

問題 No.430 文字列検索
ユーザー tottoripapertottoripaper
提出日時 2016-10-02 23:03:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,854 bytes
コンパイル時間 1,310 ms
コンパイル使用メモリ 152,148 KB
実行使用メモリ 11,668 KB
最終ジャッジ日時 2023-08-15 17:23:25
合計ジャッジ時間 2,271 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,596 KB
testcase_01 AC 45 ms
11,408 KB
testcase_02 AC 18 ms
11,544 KB
testcase_03 AC 18 ms
11,420 KB
testcase_04 AC 3 ms
5,672 KB
testcase_05 AC 2 ms
5,664 KB
testcase_06 AC 2 ms
5,596 KB
testcase_07 AC 2 ms
5,676 KB
testcase_08 AC 41 ms
11,528 KB
testcase_09 AC 2 ms
5,612 KB
testcase_10 AC 4 ms
6,056 KB
testcase_11 AC 36 ms
11,544 KB
testcase_12 AC 36 ms
11,480 KB
testcase_13 AC 38 ms
11,548 KB
testcase_14 AC 36 ms
11,400 KB
testcase_15 AC 31 ms
11,484 KB
testcase_16 AC 21 ms
11,400 KB
testcase_17 AC 19 ms
11,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};


template <typename T>
T expt(T a, T n, T mod = std::numeric_limits<T>::max()){
    T res = 1;
    while(n){
        if(n & 1){res = res * a % mod;}
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

string S, C[5000];
int M;
ll _hash[11][50000];
vector<ll> hash_v[11];

int main(){
    //*
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    /*/
      /*/

    std::cin >> S;

    int s_length = S.size();
    for(int i=1;i<=10;++i){        
        for(int j=0;j<s_length;++j){
            _hash[i][j] = (j > 0 ? _hash[i][j-1] * 26 : 0) + S[j] - 'A';
            if(j - i >= 0){
                _hash[i][j] -= 1ll * (S[j-i] - 'A') * expt(26ll, 1ll * i);
            }
        }

        for(int j=i-1;j<s_length;++j){
            hash_v[i].emplace_back(_hash[i][j]);
        }

        sort(hash_v[i].begin(), hash_v[i].end());
    }

    std::cin >> M;

    ll res = 0;
    for(int i=0;i<M;++i){
        std::cin >> C[i];

        ll h = 0;
        for(const auto& c : C[i]){
            h = h * 26 + c - 'A';
        }

        // std::cout << "Hashs" << std::endl;
        // for(int x : hash_v[C[i].size()]){
        //     std::cout << x << std::endl;
        // }
        // std::cout << "Hitomazu" << std::endl;

        auto it = lower_bound(hash_v[C[i].size()].begin(), hash_v[C[i].size()].end(), h),
            it2 = upper_bound(hash_v[C[i].size()].begin(), hash_v[C[i].size()].end(), h);

        if(it != hash_v[C[i].size()].end() && *it == h){
            res += it2 - it;
        }
    }

    std::cout << res << std::endl;
}
0