結果

問題 No.430 文字列検索
ユーザー AC2KAC2K
提出日時 2022-12-01 17:39:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,164 bytes
コンパイル時間 2,746 ms
コンパイル使用メモリ 170,052 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-17 06:53:15
合計ジャッジ時間 5,155 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 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;
//#include<atcoder/all>
//using namespace atcoder;
//cout << fixed << setprecision(10);
#define rep(i, N)  for(int i=0;i<(N);i++)
#define all(x) (x).begin(),(x).end()
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 ll MOD = 1e9 + 7;
const int dx[4] = { 1,0,-1,0 };
const int dy[4] = { 0,1,0,-1 };



class Hash {
private:
    /*メンバ*/
    ll mod = 998244353;
    ll base = 32;
    string s;
    vector<ll> hash;
    /*modint関連のメソッドを持っておく*/
    ll mod_pow(ll base, ll exp)
    {
        if (exp == 0)
            return 1;
        if (exp == 1)
            return base % mod;
        ll half = mod_pow(base, exp / 2);
        ll ret = (half * half) % mod;
        if (exp % 2 == 1)
            ret = ret * base % mod;
        return ret;
    }
    int id(char c) { return c - 'a' + 1; }
public:
    /*コンストラクタ*/
    Hash(string _s, ll _mod = 998244353, ll _base = 100) :s(_s), mod(_mod), base(_base), hash(_s.size() + 1) {    }

    /*メソッド*/

    /*ハッシュの表を作る*/
    void build() {
        hash[0] = 0;
        for (int i = 0; i < s.size(); i++) {
            hash[i + 1] = (hash[i] * base % mod + id(s[i]) % mod) % mod;
        }
    }

    /*区間ハッシュを求める*/

    ll rangeHash(int l,int r) {
        return (hash[r] - mod_pow(base, r - l + 1) * hash[l - 1] % mod + mod) % mod;    //hash[l,r]=hash[r]-base^(r-l+1)*hash[l-1]
    }

    bool same(int l, int r, int L, int R) {
        bool check = (rangeHash(l, r) == rangeHash(L, R));
        return check;
    }
};

int main() {
    string S;
    cin >> S;
    Hash S1(S);
    S1.build();
    int M;
    cin >> M;
    int ans = 0;
    while (M--) {
        string c;
        cin >> c;
        int siz = c.size();
        Hash S2(c);
        S2.build();
        ll val2 = S2.rangeHash(1, siz);
        for (int i = 1; i + siz - 1 <= S.size(); i++) {
            ll val = S1.rangeHash(i, i + siz - 1);
            if (val == val2)ans++;
        }
    }
    cout << ans << endl;
}
0