結果

問題 No.430 文字列検索
ユーザー yuji9511yuji9511
提出日時 2019-09-17 00:21:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,914 ms / 2,000 ms
コード長 2,223 bytes
コンパイル時間 1,699 ms
コンパイル使用メモリ 149,696 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 08:14:13
合計ジャッジ時間 21,799 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1,911 ms
4,376 KB
testcase_02 AC 1,914 ms
4,380 KB
testcase_03 AC 1,910 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 10 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 1,913 ms
4,380 KB
testcase_12 AC 1,914 ms
4,376 KB
testcase_13 AC 1,912 ms
4,380 KB
testcase_14 AC 1,909 ms
4,380 KB
testcase_15 AC 1,914 ms
4,380 KB
testcase_16 AC 1,908 ms
4,376 KB
testcase_17 AC 1,907 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x,y) cout << (x) << " " << (y) << endl;
#define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " \n"[i==n-1];};
struct HashData {
	int h1, h2;
	bool operator==(const HashData& rhs) const {
		return h1 == rhs.h1 && h2 == rhs.h2;
	}
	bool operator!=(const HashData& rhs) const { return !(*this == rhs); }
};

class RollingHash {
	template<int B, int M>
	class RollingHashPart {
	private:
		size_t len;
		std::vector<int> pow_b, hash;
	public:
		RollingHashPart() {}
		template<typename T>
		RollingHashPart(const T& s) : len(s.size()), pow_b(s.size() + 1), hash(s.size() + 1) {
			pow_b[0] = 1;
			hash[0] = 0;
			for (int i = 0; i < len; i++) {
				pow_b[i + 1] = (ll)pow_b[i] * B % M;
				hash[i + 1] = ((ll)hash[i] * B + s[i]) % M;
			}
		}
		// [l, r)
		int get(int l, int r) const {
			int res = (hash[r] - (ll)hash[l] * pow_b[r - l]) % M;
			if (res < 0) res += M;
			return res;
		}
	};
public:
	RollingHashPart<1033, 1000000007> rh1;
	RollingHashPart<6037, 999999937> rh2;
 
	RollingHash() {}
	template<typename T> RollingHash(const T & s) : rh1(s), rh2(s) {}
	// [l, r)
	HashData get(int l, int r) const {
		return (HashData){ rh1.get(l, r), rh2.get(l, r) };
	}
	// [l1, r1), [l2, r2)
	int lcp(const RollingHash & rhs, int l1, int r1, int l2, int r2) const {
		int lb = 0, ub = std::min(r1 - l1, r2 - l2) + 1;
		while (ub - lb > 1) {
			int md = (lb + ub) >> 1;
			(get(l1, l1 + md) == rhs.get(l2, l2 + md) ? lb : ub) = md;
		}
		return lb;
	}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    ll M;
    cin >> M;
    string C[5010];
    rep(i,0,M) cin >> C[i];
    ll ans = 0;
    ll N = S.size();
    RollingHash rh(S);
    rep(i,0,M){
        RollingHash rh2(C[i]);
        ll sz = C[i].size();
        rep(j,0,N){
            if(rh.get(j, j+sz) == rh2.get(0,sz)){
                ans++;
            }
        }
    }
    print(ans);
}
0