結果
問題 | No.430 文字列検索 |
ユーザー | Pachicobue |
提出日時 | 2019-09-30 23:50:37 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 167 ms / 2,000 ms |
コード長 | 6,709 bytes |
コンパイル時間 | 2,937 ms |
コンパイル使用メモリ | 227,520 KB |
実行使用メモリ | 8,860 KB |
最終ジャッジ日時 | 2024-11-10 00:35:14 |
合計ジャッジ時間 | 5,179 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,248 KB |
testcase_01 | AC | 167 ms
8,860 KB |
testcase_02 | AC | 142 ms
5,720 KB |
testcase_03 | AC | 140 ms
5,340 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 137 ms
5,248 KB |
testcase_09 | AC | 3 ms
5,248 KB |
testcase_10 | AC | 15 ms
5,248 KB |
testcase_11 | AC | 158 ms
6,508 KB |
testcase_12 | AC | 157 ms
6,756 KB |
testcase_13 | AC | 156 ms
6,880 KB |
testcase_14 | AC | 155 ms
6,140 KB |
testcase_15 | AC | 144 ms
5,564 KB |
testcase_16 | AC | 142 ms
6,204 KB |
testcase_17 | AC | 141 ms
6,332 KB |
ソースコード
#include <bits/stdc++.h> #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" #define NDEBUG using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T msbp1(const T u) { return log2p1(u); } template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); } template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; } template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); } template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); } template<typename T> constexpr bool btest(const T mask, const usize ind) { return ((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); } template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); } template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4; template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template<typename T> T read() { T v; return std::cin >> v, v; } template<typename T> std::vector<T> read_vec(const std::size_t size) { std::vector<T> v(size); for (auto& e : v) { std::cin >> e; } return v; } template<typename... Types> auto read_vals() { return std::tuple<std::decay_t<Types>...>{read<Types>()...}; } #define SHOW(...) static_cast<void>(0) template<typename T> std::vector<T> make_v(const std::size_t size, T v) { return std::vector<T>(size, v); } template<class... Args> auto make_v(const std::size_t size, Args... args) { return std::vector<decltype(make_v(args...))>(size, make_v(args...)); } class trie { public: struct node_t { usize sub = 0; std::vector<usize> accept; std::map<char, usize> child; }; trie() : nodes{node_t{}} {} const node_t& operator[](const usize ind) const { return nodes[ind]; } void add(const std::string& s) { const usize sind = nodes[0].sub++; for (usize v = 0, i = 0; i < s.size(); i++) { const char c = s[i]; if (nodes[v].child.find(c) == nodes[v].child.end()) { nodes[v].child[c] = new_node(); } v = nodes[v].child.at(c), nodes[v].sub++; if (i + 1 == s.size()) { nodes[v].accept.push_back(sind); } } } std::vector<usize> path_to(const std::string& s) const { std::vector<usize> ans{0}; for (usize v = 0, i = 0; i < s.size(); i++) { const char c = s[i]; if (nodes[v].child.find(c) == nodes[v].child.end()) { break; } v = nodes[v].child.at(c), ans.push_back(v); } return ans; } friend std::ostream& operator<<(std::ostream& os, const trie& tr) { auto rec = [&](auto&& self, const usize v, const char c) -> void { os << "(" << c; for (const auto& q : tr.nodes[v].child) { self(self, q.second, q.first); } os << ")"; }; return rec(rec, 0, ' '), os; } usize count() const { return nodes.front().sub; } usize size() const { return nodes.size(); } private: usize new_node() { return nodes.push_back(node_t{}), nodes.size() - 1; } std::vector<node_t> nodes; }; class aho_corasick { public: aho_corasick(const trie& tr) : tr{tr}, accept(tr.size()), failure(accept.size(), accept.size()) { const usize sz = accept.size(); std::queue<usize> q; std::string label(sz, ' '); std::vector<usize> par(sz, sz); q.push(0); while (not q.empty()) { const usize v = q.front(); q.pop(); for (const auto& p : tr[v].child) { label[p.second] = p.first, par[p.second] = v, q.push(p.second); } if (v == 0) { continue; } usize u = failure[par[v]]; const char c = label[v]; for (; u < sz and tr[u].child.find(c) == tr[u].child.end(); u = failure[u]) {} failure[v] = (u == sz ? 0UL : tr[u].child.at(c)); } } usize operator[](const usize i) const { return failure[i]; } std::vector<std::vector<usize>> match(const std::string& s, const bool skip_precalc = false) // 末尾位置 { if (not skip_precalc and not precalced) { precalc(); } const usize sz = tr.size(); std::vector<std::vector<usize>> ans(tr.count()); for (usize v = 0, i = 0; i < s.size(); i++) { const char c = s[i]; for (; v < sz and tr[v].child.find(c) == tr[v].child.end(); v = failure[v]) {} v = (v == sz ? 0UL : tr[v].child.at(c)); std::cerr << v << std::endl; for (const usize si : accept[v]) { ans[si].push_back(i); } } return ans; } private: void precalc() { precalced = true; std::queue<usize> q; q.push(0); while (not q.empty()) { const usize v = q.front(), f = failure[v]; q.pop(); for (const auto& p : tr[v].child) { q.push(p.second); } if (v == 0) { continue; } std::set_union(tr[v].accept.begin(), tr[v].accept.end(), accept[f].begin(), accept[f].end(), std::back_inserter(accept[v])); } } const trie& tr; bool precalced = false; std::vector<std::vector<usize>> accept; std::vector<usize> failure; }; int main() { const auto s = read<std::string>(); const auto n = read<usize>(); const auto c = read_vec<std::string>(n); trie tr; for (usize i = 0; i < n; i++) { tr.add(c[i]); } aho_corasick aho(tr); const auto p = aho.match(s); usize ans = 0; for (usize i = 0; i < n; i++) { ans += p[i].size(); } std::cout << ans << std::endl; return 0; }