結果

問題 No.430 文字列検索
ユーザー ldsybldsyb
提出日時 2017-11-27 17:38:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 875 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 168,352 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 07:24:21
合計ジャッジ時間 6,938 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 423 ms
4,376 KB
testcase_02 AC 421 ms
4,376 KB
testcase_03 AC 422 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 421 ms
4,380 KB
testcase_12 AC 421 ms
4,376 KB
testcase_13 AC 421 ms
4,376 KB
testcase_14 AC 424 ms
4,376 KB
testcase_15 AC 420 ms
4,380 KB
testcase_16 AC 420 ms
4,376 KB
testcase_17 AC 421 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct rolling_hash {
	string s;
	vector<uint64_t> hash, bp;
	uint64_t b = 1007;
	int n;
	
	rolling_hash(const string &s) : s(s), n(s.size()) {
		hash.resize(n + 1);
		bp.resize(n + 1);
		hash[0] = 0;
		bp[0] = 1;
		for (int i = 0; i < n; i++) {
			hash[i + 1] = hash[i] * b + s[i];
			bp[i + 1] = bp[i] * b;
		}
	}
	
	//[l, r)
	uint64_t get_hash(int l, int r) {
		assert(0 <= l && l < r && r <= n);
		return hash[r] - bp[r - l] * hash[l];
	}
};

int main() {
	string s;
	cin >> s;
	rolling_hash s_hash(s);
	int m;
	cin >> m;
	int ans = 0;
	for (int i = 0; i < m; i++) {
		string t;
		cin >> t;
		rolling_hash t_hash(t);
		uint64_t puni = t_hash.get_hash(0, t.size());
		for (int j = 0; j + t.size() <= s.size(); j++) {
			if (puni == s_hash.get_hash(j, j + t.size())) {
				ans++;
			}
		}
	}
	cout << ans << endl;
	return 0;
}
0