結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-11-21 22:19:13 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 153 ms / 2,000 ms | 
| コード長 | 2,487 bytes | 
| コンパイル時間 | 1,076 ms | 
| コンパイル使用メモリ | 112,312 KB | 
| 実行使用メモリ | 27,264 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:38:29 | 
| 合計ジャッジ時間 | 2,121 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#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;
}
            
            
            
        