結果

問題 No.430 文字列検索
ユーザー kaikeykaikey
提出日時 2020-08-11 00:29:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,046 ms / 2,000 ms
コード長 2,777 bytes
コンパイル時間 1,683 ms
コンパイル使用メモリ 170,636 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 09:18:09
合計ジャッジ時間 12,802 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1,042 ms
5,376 KB
testcase_02 AC 1,044 ms
5,376 KB
testcase_03 AC 1,036 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 8 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 1,036 ms
5,376 KB
testcase_12 AC 1,042 ms
5,376 KB
testcase_13 AC 1,038 ms
5,376 KB
testcase_14 AC 1,034 ms
5,376 KB
testcase_15 AC 1,046 ms
5,376 KB
testcase_16 AC 1,038 ms
5,376 KB
testcase_17 AC 1,042 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include <random>
using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define endk '\n'
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
const lint mod = 1e9 + 7, INF = 1e18;
lint dx[8] = { 0, -1, 1, 0, 1, -1, 1, -1 }, dy[8] = { 1, 0, 0, -1, -1, -1, 1, 1 };

struct RollingHash {
	const _ulong MASK30 = (1ULL << 30) - 1;
	const _ulong MASK31 = (1ULL << 31) - 1;
	const _ulong MOD = (1ULL << 61) - 1;
	const _ulong MASK61 = MOD;

	vector<_ulong> hashed, power;

	RollingHash(const string& s, _ulong base) {
		int sz = SZ(s);
		hashed.assign(sz + 1, 0);
		power.assign(sz + 1, 0);
		power[0] = 1;
		for (int i = 0; i < sz; i++) {
			power[i + 1] = mul(power[i], base);
			hashed[i + 1] = mul(hashed[i], base) + s[i];
			if (hashed[i + 1] >= MOD) hashed[i + 1] -= MOD;
		}
	}

	//乱数生成、main関数にコピペする
	void generate_base() {
		_ulong base;
		{
			//乱数生成
			random_device seed_gen;
			mt19937 engine(seed_gen());
			uniform_real_distribution<> dist1(129, INT_MAX);
			base = dist1(engine);
		}
	}

	_ulong get(int l, int r) const {
		_ulong ret = hashed[r] + MOD - mul(hashed[l], power[r - l]);
		if (ret >= MOD) ret -= MOD;
		return ret;
	}

	//a*b mod 2^61-1を返す関数
	inline _ulong mul(_ulong a, _ulong b) const {
		_ulong u_a = a >> 31;//aの31bit以降
		_ulong d_a = a & MASK31;//aの31bit未満
		_ulong u_b = b >> 31;
		_ulong d_b = b & MASK31;

		_ulong mid = d_a * u_b + u_a * d_b;
		_ulong u_mid = mid >> 30;
		_ulong d_mid = mid & MASK30;
		return CalcMod(u_a * u_b * 2 + u_mid + (d_mid << 31) + d_a * d_b);
	}

	//mod 2^61-1を返す関数
	inline _ulong CalcMod(_ulong x) const {
		_ulong u_x = x >> 61;
		_ulong d_x = x & MASK61;
		_ulong res = u_x + d_x;
		if (res >= MOD) res -= MOD;
		return res;
	}
};

string s, t;
lint M;
int main() {
	_ulong base;
	{
		//乱数生成
		random_device seed_gen;
		mt19937 engine(seed_gen());
		uniform_real_distribution<> dist1(129, INT_MAX);
		base = dist1(engine);
	}
	cin >> s;
	cin >> M;
	RollingHash rh(s, base);
	lint ans = 0;
	REP(i, M) {
		cin >> t;
		RollingHash rh2(t, base);
		for (int i = 0; i + SZ(t) <= SZ(s); i++) {
			if (rh.get(i, i + SZ(t)) == rh2.get(0, SZ(t))) {
				ans++;
			}
		}
	}
	cout << ans << endk;
}
0