結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,880 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++)


struct RollingHash {
	typedef long long int_type;
	typedef pair<int_type, int_type> hash_type;

	int_type base1;
	int_type base2;
	int_type mod1;
	int_type mod2;
	vector<int_type> hash1;
	vector<int_type> hash2;
	vector<int_type> pow1;
	vector<int_type> pow2;

	RollingHash() : base1(1009), base2(1007), mod1(1000000007), mod2(1000000009) {}

	void init(const string &s) {
		int n = s.size();

		hash1.assign(n + 1, 0);
		hash2.assign(n + 1, 0);
		pow1.assign(n + 1, 1);
		pow2.assign(n + 1, 1);

		for (int i = 0; i<n; i++) {
			hash1[i + 1] = (hash1[i] + s[i]) * base1 % mod1;
			hash2[i + 1] = (hash2[i] + s[i]) * base2 % mod2;
			pow1[i + 1] = pow1[i] * base1 % mod1;
			pow2[i + 1] = pow2[i] * base2 % mod2;
		}
	}

	hash_type get(int l, int r) {
		int_type t1 = ((hash1[r] - hash1[l] * pow1[r - l]) % mod1 + mod1) % mod1;
		int_type t2 = ((hash2[r] - hash2[l] * pow2[r - l]) % mod2 + mod2) % mod2;
		return make_pair(t1, t2);
	}

	RollingHash::hash_type concat(hash_type h1, hash_type h2, int h2_len) {
		return make_pair((h1.first*pow1[h2_len] + h2.first) % mod1, (h1.second*pow2[h2_len] + h2.second) % mod2);
	}

};
//-----------------------------------------------------------------
string S;
int M;
string C[5010];
//-----------------------------------------------------------------
int main() {
	cin >> S >> M;
	rep(i, 0, M) cin >> C[i];

	RollingHash HashS;
	HashS.init(S);

	int ans = 0;
	rep(i, 0, M) {
		RollingHash HashC;
		HashC.init(C[i]);

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

	cout << ans << endl;
}
0