結果

問題 No.430 文字列検索
ユーザー finefine
提出日時 2019-11-14 02:50:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 620 ms / 2,000 ms
コード長 1,074 bytes
コンパイル時間 1,648 ms
コンパイル使用メモリ 171,268 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 19:53:54
合計ジャッジ時間 9,122 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 618 ms
4,348 KB
testcase_02 AC 616 ms
4,348 KB
testcase_03 AC 608 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 601 ms
4,348 KB
testcase_12 AC 620 ms
4,348 KB
testcase_13 AC 602 ms
4,348 KB
testcase_14 AC 618 ms
4,348 KB
testcase_15 AC 617 ms
4,348 KB
testcase_16 AC 606 ms
4,348 KB
testcase_17 AC 606 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ull = unsigned long long;

template<ull mod = 1000000007>
struct RollingHash {
	vector<ull> hashs, power;

	RollingHash() {}
	RollingHash(const string& s, ull base = 10007) {
		int sz = s.size();
		hashs.assign(sz + 1, 0);
		power.assign(sz + 1, 0);
		power[0] = 1;

		for (int i = 0; i < sz; i++) {
			power[i + 1] = power[i] * base % mod;
			hashs[i + 1] = (hashs[i] * base) % mod + s[i];
			if (hashs[i + 1] >= mod) hashs[i] -= mod;
		}
	}

	ull get(int l, int r) const {
		ull res = hashs[r] + mod - hashs[l] * power[r - l] % mod;
		if (res >= mod) res -= mod;
		return res;
	}
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s;
	cin >> s;
	int n = s.size();
	int m;
	cin >> m;
	RollingHash<> rhs(s);
	int ans = 0;
	for (int i = 0; i < m; i++) {
		string c;
		cin >> c;
		RollingHash<> rhc(c);
		int cl = c.size();
		int cnt = 0;
		for (int i = 0; i + cl <= n; i++) {
			cnt += (rhs.get(i, i + cl) == rhc.get(0, cl));
		}
		ans += cnt;
	}
	cout << ans << "\n";
	return 0;
}
0