結果

問題 No.430 文字列検索
ユーザー AC2KAC2K
提出日時 2023-01-13 22:30:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,705 bytes
コンパイル時間 2,653 ms
コンパイル使用メモリ 210,032 KB
実行使用メモリ 169,292 KB
最終ジャッジ日時 2023-08-26 04:14:15
合計ジャッジ時間 5,792 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,768 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//cout << fixed << setprecision(10);
#define rep(i, N)  for(int i=0;i<(N);i++)
#define all(x) (x).begin(),(x).end()
#define popcount(x) __builtin_popcount(x)
using ll = long long;
using ld = long double;
using graph = vector<vector<int>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll infl = 1e18;
const ld eps = ld(0.000000001);
const long double pi = acos(-1);
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const int dx[4] = { 1,0,-1,0 };
const int dy[4] = { 0,1,0,-1 };
template<class T>void chmax(T&x,T y){if(x<y)x=y;}
template<class T>void chmin(T&x,T y){if(x>y)x=y;}

class RollingHash {
    static const ll mod = 1e9 + 7;
    const ll base;
    vector<ll> powers;	//base^i
    static inline ll generate_base() {
        mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());
        uniform_int_distribution<ll> rand((ll)1, (ll)mod - 1);
        return rand(engine);
    }
    ll mod_pow(ll base, ll exp) {
        if(base==0)return 0;
        ll ans = 1;
        base %= mod;
        while (exp > 0) {
            if (exp & 1) {
                ans *= base;
                ans %= mod;
            }
            base *= base;
            base %= mod;
            exp >>= 1;
        }
        return ans;
    }
    //idの振り方
    ll mapping(char c) {
        return (c - 'A' + 1);
    }
    void expand(int siz) {
        if (powers.size() < siz + 1) {
            int pre_siz = powers.size();
            powers.resize(siz + 1);
            for (int i = pre_siz; i <= siz; i++) {
                powers[i] = (powers[i - 1] * base) % mod;
            }
        }
    }

public:
    RollingHash(ll base = generate_base()) :base(generate_base()), powers{ 1 } {	} 
    vector<ll> build(string& s) {
        vector<ll> hash(s.size() + 1);
        for (int i = 1; i <= s.size(); i++) {
            hash[i] = (hash[i-1] * base % mod + mapping(s[i-1])) % mod;
        }
        return hash;
    }

    ll range(vector<ll>&hash,int l, int r) {
        //expand(r - l);
        return ((hash[r] + mod - hash[l] * mod_pow(base,r - l)) % mod + mod) % mod;
    }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin>>s;
    int m;
    cin>>m;
    vector<ll> c_hash(m);
    RollingHash rh;
    rep(i,m){
        string c;
        cin>>c;
        vector<ll> tmp=rh.build(c);
        c_hash[i]=tmp.back();
    }

    int len=s.size();
    auto res=rh.build(s);
    unordered_map<ll,ll> cnt;
    for(int i=0;i<len;i++)for(int j=i+1;j<=len;j++){
        ll hash=rh.range(res,i,j);
        cnt[hash]++;
    }

    int ans=0;
    rep(i,m){
        ans+=cnt[c_hash[i]];
    }
    cout<<ans<<endl;
}
0