結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-01-14 04:15:15 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 65 ms / 2,000 ms | 
| コード長 | 3,216 bytes | 
| コンパイル時間 | 1,332 ms | 
| コンパイル使用メモリ | 131,720 KB | 
| 実行使用メモリ | 6,912 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:40:07 | 
| 合計ジャッジ時間 | 2,330 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#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;
}
            
            
            
        