結果

問題 No.430 文字列検索
ユーザー idat_50meidat_50me
提出日時 2023-10-08 12:34:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 2,401 bytes
コンパイル時間 2,274 ms
コンパイル使用メモリ 215,940 KB
実行使用メモリ 27,884 KB
最終ジャッジ日時 2023-10-08 12:34:13
合計ジャッジ時間 4,175 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 263 ms
27,884 KB
testcase_02 AC 24 ms
5,160 KB
testcase_03 AC 25 ms
5,220 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 255 ms
27,392 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 17 ms
5,868 KB
testcase_11 AC 101 ms
8,924 KB
testcase_12 AC 101 ms
8,936 KB
testcase_13 AC 102 ms
8,904 KB
testcase_14 AC 82 ms
7,328 KB
testcase_15 AC 65 ms
6,492 KB
testcase_16 AC 29 ms
5,224 KB
testcase_17 AC 27 ms
5,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/430

#ifndef call_include
#define call_include
#include <bits/stdc++.h>
using namespace std;
#endif

struct RollingHash {
private:
	const int N;
	const long long MOD1 = 1000000007;
	const long long MOD2 = 1992732529;
	random_device rd;
	mt19937 mt = mt19937(rd());
	uniform_int_distribution<long long> dist1, dist2;
	const long long base1, base2;
	vector<long long> hash1, hash2, pw1, pw2;

public:
	RollingHash(string &s) :
		N(s.size()),
		dist1(257, MOD1 - 2),
		dist2(257, MOD2 - 2),
		base1(dist1(mt)),
		base2(dist2(mt)),
		hash1(N + 1, 0),
		hash2(N + 1, 0),
		pw1(N + 1, 1),
		pw2(N + 1, 1) {
		for(int i = 0; i < N; i++) {
			pw1[i + 1] = pw1[i] * base1 % MOD1;
			pw2[i + 1] = pw2[i] * base2 % MOD2;
			hash1[i + 1] = hash1[i] * base1 % MOD1 + int(s[i]);
			hash2[i + 1] = hash2[i] * base2 % MOD2 + int(s[i]);
			if(hash1[i + 1] >= MOD1) hash1[i + 1] -= MOD1;
			if(hash2[i + 1] >= MOD2) hash2[i + 1] -= MOD2;
		}
	}

	pair<long long, long long> get(int l, int r) {
		assert(l <= r);
		assert(0 <= l and r <= N);
		long long h1 = hash1[r] - hash1[l] * pw1[r - l] % MOD1;
		long long h2 = hash2[r] - hash2[l] * pw2[r - l] % MOD2;
		if(h1 < 0) h1 += MOD1;
		if(h2 < 0) h2 += MOD2;
		return pair(h1, h2);
	}

	pair<long long, long long> get(string &t) {
		const int M = t.size();
		long long h1_t = 0, h2_t = 0;
		for(int i = 0; i < M; i++) {
			h1_t = h1_t * base1 % MOD1 + int(t[i]);
			h2_t = h2_t * base2 % MOD2 + int(t[i]);
			if(h1_t >= MOD1) h1_t -= MOD1;
			if(h2_t >= MOD2) h2_t -= MOD2;
		}
		return pair(h1_t, h2_t);
	}

	vector<int> iscontain(string &t) {
		const int M = t.size();
		if(N < M) return {};

		vector<int> ret;
		pair<long long, long long> pt = get(t);
		for(int i = 0; i + M <= N; i++) {
			pair<long long, long long> p = get(i, i + M);
			if(p.first == pt.first and p.second == pt.second) ret.emplace_back(i);
		}

		return ret;
	}
};

int main() {
	string S;
	int M;
	vector<string> C;

	cin >> S >> M;
	C.resize(M);
	for(int i = 0; i < M; i++) cin >> C[i];

	RollingHash rh(S);
	map<pair<long long, long long>, long long> m;
	for(int i = 0; i < S.size(); i++) {
		for(int n = 1; n <= 10; n++) {
			if(i + n <= S.size()) m[rh.get(i, i + n)]++;
		}
	}
	long long ans = 0;
	for(int i = 0; i < M; i++) {
		pair<long long, long long> hasht = rh.get(C[i]);
		ans += m[hasht];
	}

	cout << ans << endl;
}
0