From c5ff11cf9d1ba982af6b9f4b89a19cf893b909d1 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Fri, 24 Mar 2023 23:30:39 +0800 Subject: [PATCH] add: 230324-CN --- cpp/2303/230324-CN.cpp | 77 +++++++++++++++++++++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230324-CN.cpp diff --git a/cpp/2303/230324-CN.cpp b/cpp/2303/230324-CN.cpp new file mode 100644 index 0000000..14d48d6 --- /dev/null +++ b/cpp/2303/230324-CN.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +class KMP { +private: + std::string str; + std::vector m; + int pos = 0; + +public: + explicit KMP(std::string s) : str(std::move(s)), m(str.length()) { + for (int i = 1; i < str.length(); ++i) { + int j = m[i - 1]; + for (; j > 0 && str[j] != str[i]; j = m[j - 1]); + m[i] = j + (str[i] == str[j]); + } + } + + const std::string& get_string() const { + return str; + } + + bool advance(char ch) { + if (ch == str[pos]) { + ++pos; + } else { + for (; pos > 0 && str[pos] != ch; pos = m[pos - 1]); + pos += str[pos] == ch; + } + if (pos == str.length()) { + pos = m[pos - 1]; + return true; + } + return false; + } +}; + +/** + * 1032. Stream of Characters + * + * Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings words. + * + * For example, if words = ["abc", "xyz"] and the stream added the four characters (one by one) 'a', 'x', 'y', and 'z', your algorithm should detect that the suffix "xyz" of the characters "axyz" matches "xyz" from words. + * + * Implement the StreamChecker class: + * + * StreamChecker(String[] words) Initializes the object with the strings array words. + * boolean query(char letter) Accepts a new character from the stream and returns true if any non-empty suffix from the stream forms a word that is in words. + */ + +class StreamChecker { +private: + std::vector sms; + +public: + explicit StreamChecker(const std::vector& words) { + sms.reserve(words.size()); + for (const std::string& i : words) + sms.emplace_back(i); + } + + bool query(char c) { + bool ret = false; + for (KMP& sm : sms) + ret |= sm.advance(c); + return ret; + } +}; + +int main() { + StreamChecker sc({"acacac"}); + for (int i = 0; i < 4; ++i) { + std::cout << sc.query('a') << sc.query('c'); + } + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index de407fb..1045212 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230323.cpp) +ADD_EXECUTABLE(2303 230324-CN.cpp)