結果
問題 | No.430 文字列検索 |
ユーザー | naskya |
提出日時 | 2021-03-27 17:14:18 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,009 ms / 2,000 ms |
コード長 | 16,392 bytes |
コンパイル時間 | 7,718 ms |
コンパイル使用メモリ | 363,676 KB |
実行使用メモリ | 24,448 KB |
最終ジャッジ日時 | 2024-11-10 00:55:05 |
合計ジャッジ時間 | 16,264 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 8 ms
9,088 KB |
testcase_01 | AC | 1,009 ms
24,320 KB |
testcase_02 | AC | 881 ms
24,320 KB |
testcase_03 | AC | 890 ms
24,320 KB |
testcase_04 | AC | 9 ms
9,216 KB |
testcase_05 | AC | 8 ms
9,216 KB |
testcase_06 | AC | 8 ms
9,088 KB |
testcase_07 | AC | 8 ms
9,216 KB |
testcase_08 | AC | 31 ms
24,320 KB |
testcase_09 | AC | 9 ms
9,344 KB |
testcase_10 | AC | 12 ms
10,368 KB |
testcase_11 | AC | 948 ms
24,320 KB |
testcase_12 | AC | 862 ms
24,320 KB |
testcase_13 | AC | 884 ms
24,448 KB |
testcase_14 | AC | 864 ms
24,448 KB |
testcase_15 | AC | 909 ms
24,320 KB |
testcase_16 | AC | 916 ms
24,320 KB |
testcase_17 | AC | 978 ms
24,320 KB |
ソースコード
#pragma region template // clang-format off #if (defined __INTELLISENSE__) && (!defined PROPER) #define NDEBUG namespace std { #endif #ifdef LOCAL_NDEBUG #include <headers.hpp> #endif #ifdef LOCAL_DEBUG #include <debugger.hpp> #endif #include <cassert> #include <cctype> #include <cmath> #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <array> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <iomanip> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <typeinfo> #include <type_traits> #include <utility> #include <vector> #if (defined __INTELLISENSE__) && (!defined PROPER) using namespace std; } #endif #ifdef DEBUGGER_HPP_INCLUDED #define FILE_LINE "[Debug] ./" + std::string(__FILE__) + ":" + std::to_string(__LINE__) + " in " + std::string(__func__) #define see(...) debugger::multi_print(#__VA_ARGS__, __VA_ARGS__) #define seetype(arg) debugger::multi_print(#arg, debugger::type_name<decltype(arg)>) #define here(...) do {debugger::output << FILE_LINE << ": \e[32mReached\e[39m\n"; see(__VA_ARGS__); debugger::output << (std::string(#__VA_ARGS__).empty() ? "" : "\n");} while(0) #define com(msg) debugger::output << FILE_LINE << ":\n \e[36mComment:\e[39m " << msg << "\n" #define local(x) do {x} while(0) #define alter(x, y) x #else #define see(...) (static_cast<void>(0)) #define seetype(arg) (static_cast<void>(0)) #define here(...) (static_cast<void>(0)) #define com(msg) (static_cast<void>(0)) #define local(x) (static_cast<void>(0)) #define alter(x, y) y #endif #if (defined DEBUGGER_HPP_INCLUDED) && (!defined NOWARN) #define warn(msg) debugger::output << FILE_LINE << ":\n \e[33mWarning:\e[39m " << msg << "\n" #else #define warn(msg) (static_cast<void>(0)) #endif #if (defined LOCAL_DEBUG) || (defined LOCAL_NDEBUG) || (defined __INTELLISENSE__) #define NOEXCEPT #define M_assert(expr) assert(expr) #define O_assert(expr) assert(expr) #else #define NOEXCEPT noexcept #define M_assert(expr) do {if(__builtin_expect(!(expr), 0)) {auto p = static_cast<std::int64_t*>(std::malloc(1 << 30)); for (int i = 0; i < (1 << 27); p[i] = 1, i += (1 << 9)); std::cerr << (*p);}} while(0) #define O_assert(expr) do {if(__builtin_expect(!(expr), 0)) {const auto X = std::string(1000, '-'); for(int i = 0; i < (1 << 18); i++) std::cout << X;}} while(0) #endif #define rep(i, n) for (std::make_signed_t<std::decay_t<decltype(n)>> i = 0; i < (n); ++i) #define rng(i, f, t, s) for (std::make_signed_t<std::decay_t<std::common_type_t<decltype(f), decltype(t)>>> i = (f); ((s) > 0) ? (i < (t)) : (i > (t)); i += (s)) #define erng(i, f, t, s) for (std::make_signed_t<std::decay_t<std::common_type_t<decltype(f), decltype(t)>>> i = (f); ((s) > 0) ? (i <= (t)) : (i >= (t)); i += (s)) [[maybe_unused]] constexpr int INF = 1000000005; [[maybe_unused]] constexpr long long LINF = 1000000000000000005LL; [[maybe_unused]] constexpr double EPS = 1e-9; [[maybe_unused]] constexpr long double LEPS = 1e-14L; [[maybe_unused]] constexpr int dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; [[maybe_unused]] constexpr int dx[9] = {0, 1, 0, -1, -1, 1, 1, -1, 0}; template <class S, class T, class... U, class V = std::common_type_t<S, T, U...>> constexpr V Min(const S a, const T b, const U... c) { if constexpr (sizeof...(U)) return std::min((V) a, (V) Min(b, c...)); else return std::min((V) a, (V) b); } template <class S, class T, class... U, class V = std::common_type_t<S, T, U...>> constexpr V Max(const S a, const T b, const U... c) { if constexpr (sizeof...(U)) return std::max((V) a, (V) Max(b, c...)); else return std::max((V) a, (V) b); } // clang-format on #pragma endregion #define ROLLING_HASH_MAX_LENGTH 250000 #define NUMBER_OF_BASES 3 #pragma region lib_rolling_hash namespace lib { namespace internal { template <std::size_t N> using hash_type = std::conditional_t<(30 < N), std::uint64_t, std::uint32_t>; template <std::size_t N> constexpr hash_type<N> mask = (static_cast<hash_type<N>>(1) << N) - 1; template <std::size_t N> constexpr hash_type<N> mul_mod_1(const hash_type<N> lhs, const hash_type<N> rhs) { const hash_type<N> lhs_u = lhs >> N, lhs_d = lhs & mask<N>; const hash_type<N> rhs_u = rhs >> N, rhs_d = rhs & mask<N>; const hash_type<N> mid = lhs_d * rhs_u + lhs_u * rhs_d; return ((lhs_u * rhs_u) << 1) + lhs_d * rhs_d + ((mid & mask<N - 1>) << N) + (mid >> (N - 1)); } template <std::size_t N, class Tp1, class Tp2> constexpr Tp1 mul_mod_2(const Tp1 lhs, const Tp2 rhs) { const Tp2 rhs_d = rhs & mask<N>; const Tp1 lhs_u = lhs >> N, mid = lhs_u * rhs_d; return (lhs & mask<N>) *rhs_d + ((mid & mask<N - 1>) << N) + (mid >> (N - 1)); } template <std::size_t N> constexpr hash_type<N> clamp(const hash_type<N> x) { if (const hash_type<N> res = (x >> N) + (x & mask<N>); res < mask<N>) return res; else return res - mask<N>; } template <class Tp>[[maybe_unused]] auto has_begin(int) -> decltype(std::declval<Tp>().begin(), std::true_type{}); template <class Tp>[[maybe_unused]] auto has_begin(...) -> std::false_type; template <class Tp>[[maybe_unused]] constexpr bool is_iteratable_container_v = decltype(has_begin<Tp>(0))::value; #if (defined _GLIBCXX_VALARRAY) || (defined _LIBCPP_VALARRAY) template <class Tp>[[maybe_unused]] constexpr bool is_iteratable_container_v<std::valarray<Tp>> = true; #endif } // namespace internal #if (NUMBER_OF_BASES == 1) namespace internal::rolling_hash_61 { using HashType = hash_type<61>; constexpr int MAX_N = ROLLING_HASH_MAX_LENGTH; constexpr std::uint32_t base = 1000000007; constexpr HashType padding = mask<61> * mask<std::numeric_limits<HashType>::digits - 61>; struct base_pow_constexpr { HashType val[MAX_N + 1]; constexpr base_pow_constexpr() : val() { val[0] = 1; for (int i = 1; i <= MAX_N; ++i) val[i] = clamp<61>(mul_mod_2<31>(val[i - 1], base)); } }; #if ROLLING_HASH_MAX_LENGTH > 250000 // if you want MAX_N to be greater than constexpr-loop-limit (typically 262144 = 2^18 in GCC), // you should use `const` instead of `constexpr` const base_pow_constexpr base_to_the_n; #else constexpr base_pow_constexpr base_to_the_n; #endif template <template <class...> class Container, class ElemType, std::enable_if_t<is_iteratable_container_v<Container<ElemType>>, std::nullptr_t> = nullptr> std::vector<HashType> build_hash(const Container<ElemType>& s) { static_assert(std::is_same_v<HashType, std::common_type_t<ElemType, HashType>>); const int N = static_cast<int>(s.size()); O_assert(N <= MAX_N); std::vector<HashType> res(N + 1); for (int i = 0; i < N; ++i) res[i + 1] = clamp<61>(mul_mod_2<31>(res[i], base) + static_cast<HashType>(s[i])); return res; } HashType substr_hash(const std::vector<HashType>& hash_vec, const int L_index, int cnt = std::numeric_limits<int>::max()) { const int N = static_cast<int>(hash_vec.size()); O_assert(0 <= L_index && L_index < N); local(if (N - L_index < cnt && cnt != std::numeric_limits<int>::max()) warn("substr was shortened (" << cnt << " -> " << (N - L_index) << ")");); cnt = std::min(cnt, N - L_index); return clamp<61>(hash_vec[L_index + cnt] + padding - mul_mod_1<31>(hash_vec[L_index], base_to_the_n.val[cnt])); } } // namespace internal::rolling_hash_61 #else namespace internal::rolling_hash_61 { using HashType = hash_type<61>; constexpr int MAX_N = ROLLING_HASH_MAX_LENGTH; constexpr int BASES_N = NUMBER_OF_BASES; constexpr std::uint32_t bases[5] = {1000000007, 1000000009, 1000000021, 1000000033, 1000000087}; constexpr HashType padding = mask<61> * mask<std::numeric_limits<HashType>::digits - 61>; struct base_pow_constexpr { HashType val[BASES_N][MAX_N + 1]; constexpr base_pow_constexpr() : val() { static_assert(BASES_N <= sizeof(bases) / sizeof(*bases)); for (int i = 0; i < BASES_N; ++i) val[i][0] = 1; for (int i = 0; i < BASES_N; ++i) for (int j = 0; j < MAX_N; ++j) val[i][j + 1] = clamp<61>(mul_mod_2<31>(val[i][j], bases[i])); } }; #if (ROLLING_HASH_MAX_LENGTH * NUMBER_OF_BASES > 250000) // if you want ROLLING_HASH_MAX_LENGTH * NUMBER_OF_BASES to be greater than // constexpr-loop-limit (typically 262144 = 2^18 in GCC), you should use // `const` instead of `constexpr` (because you cannot specify -fconstexpr-loop-limit=*** on online judge) const base_pow_constexpr base_to_the_n; #else constexpr base_pow_constexpr base_to_the_n; #endif template <template <class...> class Container, class ElemType, std::enable_if_t<is_iteratable_container_v<Container<ElemType>>, std::nullptr_t> = nullptr> std::vector<std::array<HashType, BASES_N>> build_hash(const Container<ElemType>& s) { static_assert(std::is_same_v<HashType, std::common_type_t<ElemType, HashType>>); const int N = static_cast<int>(s.size()); O_assert(N <= MAX_N); std::vector<std::array<HashType, BASES_N>> res(N + 1); std::fill(res.front().begin(), res.front().end(), 0); for (int i = 0; i < N; ++i) for (int j = 0; j < BASES_N; ++j) res[i + 1][j] = clamp<61>(mul_mod_2<31>(res[i][j], bases[j]) + static_cast<HashType>(s[i])); return res; } std::array<HashType, BASES_N> substr_hash(const std::vector<std::array<HashType, BASES_N>>& hash_vec, const int L_index, int cnt = std::numeric_limits<int>::max()) { const int N = static_cast<int>(hash_vec.size()); O_assert(0 <= L_index && L_index < N); local(if (N - L_index < cnt && cnt != std::numeric_limits<int>::max()) warn("substr was shortened (" << cnt << " -> " << (N - L_index) << ")");); cnt = std::min(cnt, N - L_index); std::array<HashType, BASES_N> res; for (int i = 0; i < BASES_N; ++i) res[i] = clamp<61>(hash_vec[L_index + cnt][i] + padding - mul_mod_1<31>(hash_vec[L_index][i], base_to_the_n.val[i][cnt])); return res; } } // namespace internal::rolling_hash_61 #endif // (NUMBER_OF_BASES == 1) #ifdef LOCAL_DEBUG namespace internal { template <class Tp> class segment_hash { public: #if (NUMBER_OF_BASES == 1) using type = internal::rolling_hash_61::HashType; #else using type = std::array<internal::rolling_hash_61::HashType, internal::rolling_hash_61::BASES_N>; #endif segment_hash(void) noexcept {} segment_hash(const type h, const Tp& s) : v(h), source(s) {} inline bool operator==(const segment_hash<Tp> rhs) const { const bool same_hash = (v == rhs.v), same_string = (source == rhs.source); assert(("Hash collided!", same_hash == same_string)); return same_string; } inline bool operator!=(const segment_hash<Tp> rhs) const { const bool same_hash = (v == rhs.v), same_string = (source == rhs.source); assert(("Hash collided!", same_hash == same_string)); return !same_string; } inline type hash_val(void) const noexcept { return v; } private: type v; Tp source; }; } // namespace internal template <template <class...> class Container, class ElemType, std::enable_if_t<internal::is_iteratable_container_v<Container<ElemType>>, std::nullptr_t> = nullptr> class rolling_hash { public: #if (NUMBER_OF_BASES == 1) using type = internal::rolling_hash_61::HashType; #else using type = std::array<internal::rolling_hash_61::HashType, internal::rolling_hash_61::BASES_N>; #endif rolling_hash(void) noexcept {} rolling_hash(const Container<ElemType>& s) : source(s), hash(internal::rolling_hash_61::build_hash(s)) {} // get hash of the whole string (/ vector / deque / ...) inline internal::segment_hash<Container<ElemType>> entire(void) const { return internal::segment_hash<Container<ElemType>>(hash.back(), source); } // get hash of the whole string (/ vector / deque / ...) inline internal::segment_hash<Container<ElemType>> substr(void) const { return internal::segment_hash<Container<ElemType>>(hash.back(), source); } // get hash of the substring of length len, starting at L_index inline internal::segment_hash<Container<ElemType>> substr(const int L_index, int len = std::numeric_limits<int>::max()) const { return internal::segment_hash<Container<ElemType>>(internal::rolling_hash_61::substr_hash(hash, L_index, len), Container<ElemType>(std::cbegin(source) + L_index, std::cbegin(source) + std::min(static_cast<int>(source.size()), L_index + len))); } private: std::vector<type> hash; Container<ElemType> source; }; #else namespace internal { template <class Tp> class segment_hash { public: #if (NUMBER_OF_BASES == 1) using type = internal::rolling_hash_61::HashType; #else using type = std::array<internal::rolling_hash_61::HashType, internal::rolling_hash_61::BASES_N>; #endif segment_hash(void) noexcept {} segment_hash(const type h) noexcept : v(h) {} inline bool operator==(const segment_hash<Tp> rhs) const noexcept { return v == rhs.v; } inline bool operator!=(const segment_hash<Tp> rhs) const noexcept { return v != rhs.v; } inline type hash_val(void) const noexcept { return v; } private: type v; }; } // namespace internal template <template <class...> class Container, class ElemType, std::enable_if_t<internal::is_iteratable_container_v<Container<ElemType>>, std::nullptr_t> = nullptr> class rolling_hash { public: #if (NUMBER_OF_BASES == 1) using type = internal::rolling_hash_61::HashType; #else using type = std::array<internal::rolling_hash_61::HashType, internal::rolling_hash_61::BASES_N>; #endif rolling_hash(void) noexcept {} rolling_hash(const Container<ElemType>& s) : hash(internal::rolling_hash_61::build_hash(s)) {} // get hash of the whole string (/ vector / deque / ...) inline internal::segment_hash<Container<ElemType>> entire(void) const { return internal::segment_hash<Container<ElemType>>(hash.back()); } // get hash of the whole string (/ vector / deque / ...) inline internal::segment_hash<Container<ElemType>> substr(void) const { return internal::segment_hash<Container<ElemType>>(hash.back()); } // get hash of the substring of length len, starting at L_index inline internal::segment_hash<Container<ElemType>> substr(const int L_index, int len = std::numeric_limits<int>::max()) const { return internal::segment_hash<Container<ElemType>>(internal::rolling_hash_61::substr_hash(hash, L_index, len)); } private: std::vector<type> hash; }; #endif // defined LOCAL_DEBUG #if (NUMBER_OF_BASES == 1) using hash_t = internal::rolling_hash_61::HashType; #else using hash_t = std::array<internal::rolling_hash_61::HashType, internal::rolling_hash_61::BASES_N>; #endif } // namespace lib #pragma endregion void solve() { std::string S; int M; std::cin >> S >> M; const lib::rolling_hash S_hash(S); const int S_len = static_cast<int>(S.size()); int ans = 0; std::vector precalc_hash(11, std::vector<lib::hash_t>(S_len)); erng(len, 1, 10, 1) { rep(start, S_len - len + 1) { precalc_hash[len][start] = S_hash.substr(start, len).hash_val(); } } while (M--) { std::string C; std::cin >> C; const auto C_whole_hash = lib::rolling_hash(C).entire().hash_val(); const int C_len = static_cast<int>(C.size()); rep(start, S_len - C_len + 1) { if (precalc_hash[C_len][start] == C_whole_hash) ans++; } } std::cout << ans << "\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); solve(); }