結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  tottoripaper | 
| 提出日時 | 2016-10-02 23:03:53 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 43 ms / 2,000 ms | 
| コード長 | 1,854 bytes | 
| コンパイル時間 | 1,318 ms | 
| コンパイル使用メモリ | 164,996 KB | 
| 実行使用メモリ | 11,520 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:02:56 | 
| 合計ジャッジ時間 | 2,147 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#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;
}
            
            
            
        