add: 230511-CN

This commit is contained in:
Eatswap 2023-05-12 18:45:08 +08:00
parent 44c80466fa
commit 50666fbdbd
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 51 additions and 22 deletions

45
cpp/2305/LC230511CN.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <bitset>
#include <string>
/**
* 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;

View File

@ -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

View File

@ -14,27 +14,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& 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<ULL> 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;
}