diff --git a/cpp/2305/LC230511CN.cpp b/cpp/2305/LC230511CN.cpp new file mode 100644 index 0000000..19b241a --- /dev/null +++ b/cpp/2305/LC230511CN.cpp @@ -0,0 +1,45 @@ +#include +#include + +/** + * 1016. Binary String With Substrings Representing 1 To N + * + * Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise. + * A substring is a contiguous sequence of characters within a string. + */ + +class LC230511CN { +public: + static bool queryString(const std::string& s, int n) noexcept; +}; + +bool LC230511CN::queryString(const std::string& s, int n) noexcept { + if (n > 1000) + return false; + std::bitset<1024> set; + set[s.front() & 1] = true; + const int sn = s.length(); + for (int i = 1, mask = 1, cnt; i <= 10 && i < sn; (++i), (mask = mask << 1 | 1)) { + // init + int cur = 0; + for (int j = 0; j < i; ++j) + cur = (cur << 1) | (s[j] & 1); + + cnt = 1; + set[cur] = true; + for (int j = i; j < sn && cnt < (1 << i); ++j) { + cur = ((cur << 1) | (s[j] & 1)) & mask; + if (set[cur]) + continue; + set[cur] = true; + ++cnt; + } + } + + for (int i = 1; i <= n; ++i) + if (!set[i]) + return false; + return true; +} + +using Solution = LC230511CN; diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index 4c8f95b..d6e44ee 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -113,4 +113,9 @@ public: static constexpr inline int smallestRepunitDivByK(int k) noexcept; }; +class LC230511CN { +public: + static bool queryString(const std::string& s, int n) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2305/main.cpp b/cpp/2305/main.cpp index 756d7b0..6d2c9fc 100644 --- a/cpp/2305/main.cpp +++ b/cpp/2305/main.cpp @@ -14,27 +14,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector& x) noexcept { } int main() { - using ULL = unsigned long long; - int ok = 0, not_ok = 0; - for (int i : {99, 101, 9999}) { - int sum = 0; - std::unordered_set vis; - for (int j = 1, c = 1;; (j = (j * 10) % i), ++c) { - sum = (sum + j) % i; - if (sum == 0) { - std::printf("%6d -> %d\n", i, c); - ++ok; - break; - } - auto key = (ULL(sum) << 32) + j; - if (vis.contains(key)) { - std::printf("%6d -> CANNOT\n", i); - ++not_ok; - break; - } - vis.insert(key); - } - } - std::printf("[OK] %d | [FAIL] %d\n", ok, not_ok); + std::cout << LC230511CN::queryString("0110", 3); return 0; }