結果

問題 No.430 文字列検索
ユーザー 👑 jupirojupiro
提出日時 2020-01-14 04:15:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 3,216 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 130,124 KB
実行使用メモリ 6,832 KB
最終ジャッジ日時 2023-08-25 11:06:22
合計ジャッジ時間 3,327 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 76 ms
6,656 KB
testcase_02 AC 41 ms
6,640 KB
testcase_03 AC 38 ms
6,464 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,388 KB
testcase_08 AC 30 ms
6,276 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 46 ms
6,484 KB
testcase_12 AC 42 ms
6,472 KB
testcase_13 AC 42 ms
6,632 KB
testcase_14 AC 42 ms
6,832 KB
testcase_15 AC 42 ms
6,636 KB
testcase_16 AC 41 ms
6,544 KB
testcase_17 AC 41 ms
6,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
//const long long MOD = 1000000007LL;
const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;


template <class T> struct RollingHash {
	vector<ull> hash, pows;
	ull base, mod;
	RollingHash(const T &a, ull base, ull mod = 1000000009)
		: hash(a.size() + 1), pows(a.size() + 1, 1), mod(mod), base(base) {
		for (int i = 0; i < a.size(); i++) {
			pows[i + 1] = pows[i] * base % mod;
			hash[i + 1] = hash[i] * base % mod + a[i];
			if (hash[i + 1] >= mod) hash[i + 1] -= mod;
		}
	}
	// 現在の文字列のサイズ
	int size() { return hash.size() - 1; }
	// [l, r)
	ull get(int l, int r) {
		assert(l <= r);
		ull ret = hash[r] + mod - hash[l] * pows[r - l] % mod;
		if (ret >= mod) ret -= mod;
		return ret;
	}
	void concat(const T &b) {
		int n = hash.size() - 1, m = b.size();
		pows.resize(n + m + 1);
		hash.resize(n + m + 1);
		for (int i = 0; i < m; i++) {
			pows[n + i + 1] = pows[n + i] * base % mod;
			hash[n + i + 1] = hash[n + i] * base % mod + b[i];
			if (hash[n + i + 1] >= mod) hash[n + i + 1] -= mod;
		}
	}
	void pop_back() {
		hash.pop_back();
		pows.pop_back();
	}
};

constexpr int HASH_NUM = 4;
struct bases_t {
	int use[HASH_NUM];
	int &operator[](int i) { return use[i]; }
	bases_t() {
		mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
		for (int i = 0; i < HASH_NUM; i++) use[i] = rnd() % 10000;
	}
} bases;

using multihash_t = array<int, HASH_NUM>;
template <class T = vector<int>> struct MultiRollingHash {
	vector<RollingHash<T>> rhs;
	MultiRollingHash(const T &a) {
		for (int i = 0; i < HASH_NUM; i++) {
			rhs.push_back(RollingHash<T>(a, bases[i]));
		}
	}
	multihash_t get(int l, int r) {
		multihash_t ret;
		for (int i = 0; i < HASH_NUM; i++) ret[i] = rhs[i].get(l, r);
		return ret;
	}
	int size() { return rhs[0].size(); }
	void concat(const T &b) {
		for (auto &rh : rhs) rh.concat(b);
	}
	void pop_back() {
		for (auto &rh : rhs) rh.pop_back();
	}
};
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	string s; cin >> s;
	MultiRollingHash<string> mrh(s);
	ll M; cin >> M;
	vector<map<multihash_t, ll>> vvs(11);
	for (ll i = 0; i < M; i++) {
		string t; cin >> t;
		MultiRollingHash<string> rol(t);
		vvs[t.size()][rol.get(0,t.size())]++;
	}
	ll res = 0;
	for (ll len = 1; len <= 10; len++) {
		for (ll i = 0; i <= (ll)s.size() - len; i++) {
			if (vvs[len].find(mrh.get(i, i + len)) != vvs[len].end()) res += vvs[len][mrh.get(i, i + len)];
		}
	}
	cout << res << endl;
	return 0;
}
0