結果

問題 No.430 文字列検索
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2016-10-02 22:55:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,929 bytes
コンパイル時間 1,602 ms
コンパイル使用メモリ 179,216 KB
実行使用メモリ 13,752 KB
最終ジャッジ日時 2024-05-01 09:58:22
合計ジャッジ時間 4,979 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,752 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;
#define rep(i,a,b) for(int i=a;i<b;i++)


typedef vector<int> vi;
typedef long long ll;
struct RollingHash {
	static const ll mo0 = 1000000007, mo1 = 1000000009;
	static ll mul0, mul1;
	static const ll add0 = 1000010007, add1 = 1003333331;
	static vector<ll> pmo[2];
	vector<int> s; int l; vector<ll> hash_[2];
	void init(vector<int> s) {
		this->s = s; l = s.size(); int i, j;
		hash_[0] = hash_[1] = vector<ll>(1, 0);
		if (!mul0) mul0 = 10009 + (((ll)&mul0) >> 5) % 259, mul1 = 10007 + (((ll)&mul1) >> 5) % 257;
		if (pmo[0].empty()) pmo[0].push_back(1), pmo[1].push_back(1);
		for (int i = 0; i < l; i++) hash_[0].push_back((hash_[0].back()*mul0 + add0 + s[i]) % mo0);
		for (int i = 0; i < l; i++) hash_[1].push_back((hash_[1].back()*mul1 + add1 + s[i]) % mo1);
	}
	pair<ll, ll> hash(int l, int r) { // s[l..r]
		if (l>r) return make_pair(0, 0);
		while (pmo[0].size()<r + 2)
			pmo[0].push_back(pmo[0].back()*mul0%mo0), pmo[1].push_back(pmo[1].back()*mul1%mo1);
		return make_pair((hash_[0][r + 1] + (mo0 - hash_[0][l] * pmo[0][r + 1 - l] % mo0)) % mo0,
			(hash_[1][r + 1] + (mo1 - hash_[1][l] * pmo[1][r + 1 - l] % mo1)) % mo1);
	}
};
vector<ll> RollingHash::pmo[2]; ll RollingHash::mul0, RollingHash::mul1;
//-----------------------------------------------------------------
string S;
int M;
string C[5010];
//-----------------------------------------------------------------
int main() {
	cin >> S >> M;
	rep(i, 0, M) cin >> C[i];

	RollingHash HashS;
	vi VS(S.length());
	rep(i, 0, S.length()) VS[i] = S[i] - 'a';
	HashS.init(VS);

	int ans = 0;
	rep(i, 0, M) {
		RollingHash HashC;
		vi VC(C[i].length());
		rep(j, 0, C[i].length()) VC[j] = C[i][j] - 'a';
		HashC.init(VC);

		rep(j, 0, S.length() - (C[i].length() - 1)) {
			auto s = HashS.hash(j, j + C[i].length() - 1);
			auto c = HashC.hash(0, C[i].length() - 1);
			if (s == c) ans++;
		}
	}

	cout << ans << endl;
}
0