diff --git a/algorithm/2020S/20200415/20200415.cpp b/algorithm/2020S/20200415/20200415.cpp new file mode 100644 index 00000000..923e698b --- /dev/null +++ b/algorithm/2020S/20200415/20200415.cpp @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ + +#include +#include +#include +#include + +using namespace std; + +bool check_value(unordered_map umap, vector vec1, double value); + +int main() { + int32_t n; + cin >> n; + unordered_map value_max; + int32_t max_v = INT32_MIN / 2; + for (int32_t i = 0; i < n; ++i) { + int32_t a; + int32_t b; + cin >> a >> b; + max_v = std::max(max_v, std::max(a, b)); + value_max[a] = std::max(value_max[a], b); + } + vector value1; + value1.reserve(value_max.size()); + for (const auto &i: value_max) { + value1.push_back(i.first); + } + std::sort(value1.begin(), value1.end(), std::less()); + int32_t begin = 0; + int32_t end = max_v * 2; + int32_t middle = 0; + while (begin < end) { + middle = (end - begin) / 2 + begin; + bool temp = check_value(value_max, value1, middle); + cout << middle << " " << temp << endl; + if (temp) { + begin = middle + 1; + } else { + end = middle; + } + } + cout << middle / 2; + return 0; +} + +bool check_value(unordered_map umap, vector vec1, double value) { + for (int32_t i = vec1.size() - 1; i >= 0; i--) { + auto order = std::lower_bound(vec1.begin(), vec1.end(), value - vec1[i]); + if (order == vec1.end()) { + continue; + } + if (i != order - vec1.begin() && umap[vec1[i]] + umap[vec1[order - vec1.begin()]] >= value) { + return true; + } + } + return false; +} \ No newline at end of file diff --git a/algorithm/2020S/20200415/CMakeLists.txt b/algorithm/2020S/20200415/CMakeLists.txt new file mode 100644 index 00000000..b9d9cb57 --- /dev/null +++ b/algorithm/2020S/20200415/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.16.6) +set(PROJECT_DAY 20200415) +project(${PROJECT_NAME}_${PROJECT_DAY}) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + +add_executable(${PROJECT_NAME}_${PROJECT_DAY} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_DAY}.cpp) diff --git a/algorithm/2020S/20200415/README.md b/algorithm/2020S/20200415/README.md new file mode 100644 index 00000000..c232c521 --- /dev/null +++ b/algorithm/2020S/20200415/README.md @@ -0,0 +1,4 @@ +20200415 code test + +company: [encryet company name](./company.gpg) + diff --git a/algorithm/2020S/20200415/company.gpg b/algorithm/2020S/20200415/company.gpg new file mode 100644 index 00000000..a1dca7b6 Binary files /dev/null and b/algorithm/2020S/20200415/company.gpg differ diff --git a/algorithm/2020S/20200426/20200426_1st.cpp b/algorithm/2020S/20200426/20200426_1st.cpp new file mode 100644 index 00000000..d58ce7ee --- /dev/null +++ b/algorithm/2020S/20200426/20200426_1st.cpp @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ +#include +#include +#include +#include +#include + +static constexpr const std::array command2{"PUSH", "TOP", "POP", "SIZE", "CLEAR"}; + +int32_t main() { + size_t t{0}; + std::cin >> t; + for (size_t i{0}; i < t; ++i) { + std::queue que; + size_t q{0}; + std::cin >> q; + for (size_t j{0}; j < q; ++j) { + std::string com; + int number; + std::cin >> com; + switch (std::distance(command2.begin(), + std::find(command2.begin(), command2.end(), com) + )) { + case 1: { + std::cin >> number; + que.push(number); + break; + } + case 2: { + if (que.empty()) { + std::cout << -1 << std::endl; + } else { + std::cout << que.front() << std::endl; + } + break; + } + case 3: { + if (que.empty()) { + std::cout << -1 << std::endl; + } else { + que.pop(); + } + break; + } + case 4: { + std::cout << que.size() << std::endl; + break; + } + case 5: { + que = std::queue(); + break; + } + } + } + } + return 0; +} \ No newline at end of file diff --git a/algorithm/2020S/20200426/20200426_3rd.cpp b/algorithm/2020S/20200426/20200426_3rd.cpp new file mode 100644 index 00000000..21ba06c5 --- /dev/null +++ b/algorithm/2020S/20200426/20200426_3rd.cpp @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ +#include +#include +#include +#include +#include + +int32_t main() { + int32_t n{0}; + std::cin >> n; + std::vector> a; + std::vector> b; + for (int i{0}; i < n; ++i) { + int x1{0}, x2{0}; + std::cin >> x1 >> x2; + a.emplace_back(x1, x2); + } + for (int i{0}; i < n; ++i) { + int x1{0}, x2{0}; + std::cin >> x1 >> x2; + b.emplace_back(x1, x2); + } + std::sort(a.begin(), a.end()); + std::sort(b.begin(), b.end()); + double min_value = INT32_MAX / 2; + struct compare { + bool operator()(const std::pair &value, + const int &key) { + return (value.first < key); + } + + bool operator()(const int &key, + const std::pair &value) { + return (key < value.first); + } + }; + for (const auto &i: a) { + const auto it = std::upper_bound(b.begin(), b.end(), + i.first, compare()) - b.begin(); + // int order = std::find(b.begin(), b.end(), a.at(j).first) - b.begin(); + for (int l = 0; l < 16; ++l) { + min_value = std::min( + min_value, + std::pow((i.first - b.at(std::min(it + l, static_cast(b.size() - 1))).first), 2) + + std::pow((i.second - b.at(std::min(it + l, static_cast(b.size() - 1))).second), 2)); + min_value = std::min( + min_value, + std::pow((i.first - b.at(std::max(it - l, static_cast(0))).first), 2) + + std::pow((i.second - b.at(std::max(it - l, static_cast(0))).second), 2)); + } + } + printf("%.3f", std::sqrt(min_value)); + return 0; +} \ No newline at end of file diff --git a/algorithm/2020S/20200426/20200426_4th.cpp b/algorithm/2020S/20200426/20200426_4th.cpp new file mode 100644 index 00000000..dfb7df0a --- /dev/null +++ b/algorithm/2020S/20200426/20200426_4th.cpp @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ +#include +#include +#include +#include +#include + +inline void s1_to_s2(std::stack &s1, std::stack &s2) { + while (!s1.empty()) { + s2.push(s1.top()); + s1.pop(); + } +} + +int32_t main() { + int32_t n{0}; + std::cin >> n; + std::stack stack1, stack2; + for (int i = 0; i < n; ++i) { + std::string str; + std::cin >> str; + if ("add" == str) { + int number; + std::cin >> number; + stack1.push(number); + s1_to_s2(stack2, stack1); + s1_to_s2(stack1, stack2); + } else if ("poll" == str) { + s1_to_s2(stack2, stack1); + stack1.pop(); + s1_to_s2(stack1, stack2); + } else if ("peek" == str) { + s1_to_s2(stack2, stack1); + std::cout << stack1.top() << std::endl; + s1_to_s2(stack1, stack2); + } + } + return 0; +} diff --git a/algorithm/2020S/20200426/20200426_5th.cpp b/algorithm/2020S/20200426/20200426_5th.cpp new file mode 100644 index 00000000..bf84908f --- /dev/null +++ b/algorithm/2020S/20200426/20200426_5th.cpp @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ +#include +#include +#include +#include +#include + +int main() { + uint32_t q{0}; + std::cin >> q; + for (uint32_t i{0}; i < q; ++i) { + int64_t x{0}; + int32_t k{0}; + std::cin >> x >> k; + int32_t times{0}; + std::stack sta; + while (x > 0) { + sta.push(x & 1); + x /= 2; + times++; + } + if (k >= times) { + std::cout << -1 << std::endl; + continue; + } + uint64_t will_cout = 0; + for (int32_t j{0}; j + 1 < k; ++j) { + will_cout += sta.top(); + sta.pop(); + will_cout *= 2; + } + will_cout += sta.top(); + //cout << times << endl; + std::cout << will_cout << std::endl; + } +} diff --git a/algorithm/2020S/20200426/CMakeLists.txt b/algorithm/2020S/20200426/CMakeLists.txt new file mode 100644 index 00000000..ce0e1120 --- /dev/null +++ b/algorithm/2020S/20200426/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.16.6) +set(PROJECT_DAY 20200426) +project(${PROJECT_NAME}_${PROJECT_DAY}) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + + +set(dependencies "1st" "3rd" "4th" "5th") +foreach (elementName IN LISTS dependencies) + add_executable(${PROJECT_NAME}_${PROJECT_DAY}_${elementName} + ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_DAY}_${elementName}.cpp) +endforeach () diff --git a/algorithm/2020S/20200426/README.md b/algorithm/2020S/20200426/README.md new file mode 100644 index 00000000..c9102cb5 --- /dev/null +++ b/algorithm/2020S/20200426/README.md @@ -0,0 +1,4 @@ +20200426 code test + +company: [encryet company name](./company.gpg) + diff --git a/algorithm/2020S/20200426/company.gpg b/algorithm/2020S/20200426/company.gpg new file mode 100644 index 00000000..b1a3804f Binary files /dev/null and b/algorithm/2020S/20200426/company.gpg differ diff --git a/algorithm/2020S/20200511/20200511_4th.cpp b/algorithm/2020S/20200511/20200511_4th.cpp new file mode 100644 index 00000000..fdf7615d --- /dev/null +++ b/algorithm/2020S/20200511/20200511_4th.cpp @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ + +#include +#include +#include +#include + +#ifdef CS203_DSAA_TEST_MACRO +namespace fourth_20200511{ +#endif + +using std::cin; +using std::cout; +using std::string; +using std::vector; +using std::unordered_set; +static constexpr const char end{'\n'}; + +using num_t = int32_t; + +int main() { + string init; + int32_t number; + cin >> init; + cin >> number; + int32_t command{0}; + unordered_set uset; + int32_t first{0}; + for (int32_t i{0}; i < number; ++i) { + cin >> command; + cin >> first; + switch (command) { + case 1: { + char second; + cin >> second; + init[first - 1] = second; + break; + } + case 2: { + int32_t second; + cin >> second; + for (int j = first - 1; j < second; ++j) { + uset.insert(init[j]); + } + cout << uset.size() << "\n"; + uset.clear(); + break; + } + default: { + return -1; + } + } + } + return 0; +} + +static const auto faster_streams = [] { + srand(time(nullptr)); + // use time to init the random seed + std::ios::sync_with_stdio(false); + std::istream::sync_with_stdio(false); + std::ostream::sync_with_stdio(false); + std::cin.tie(nullptr); + std::cout.tie(nullptr); + // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能. + return 0; +}(); +#ifdef CS203_DSAA_TEST_MACRO +} +#endif diff --git a/algorithm/2020S/20200511/20200511_4th_data/1.data.in b/algorithm/2020S/20200511/20200511_4th_data/1.data.in new file mode 100644 index 00000000..8a86fd8c --- /dev/null +++ b/algorithm/2020S/20200511/20200511_4th_data/1.data.in @@ -0,0 +1,7 @@ +abacaba +5 +2 1 4 +1 4 b +1 5 b +2 4 6 +2 1 7 diff --git a/algorithm/2020S/20200511/20200511_4th_data/1.data.out b/algorithm/2020S/20200511/20200511_4th_data/1.data.out new file mode 100644 index 00000000..0571a2e1 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_4th_data/1.data.out @@ -0,0 +1,3 @@ +3 +1 +2 diff --git a/algorithm/2020S/20200511/20200511_4th_test.cpp b/algorithm/2020S/20200511/20200511_4th_test.cpp new file mode 100644 index 00000000..2a6abac4 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_4th_test.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +#ifdef CS203_DSAA_TEST_MACRO + +#include +#include +#include +#include + +#include "20200511_4th.cpp" + +std::string getFilePath() noexcept { + return "./../../../../algorithm/2020S/20200511/20200511_4th_data/"; +} + +const std::string CS203_redirect::file_paths = getFilePath(); + +namespace fourth_20200511 { +using std::tie; +using std::cin; +using std::cout; +using std::tuple; +using std::vector; + +TEST_CASE("test case with sequence", "[test 4th_20200511 1]") { + CS203_sequence sequence{1, 1, 0}; // // 基础设定,[1,1] + sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in + sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out + sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out + const auto files_name = sequence.get_files(true); + // 获取一个std::tuple , + // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名. + for (const auto &file_name: files_name) { + const auto &[datain, dataout, testout] = file_name; + { + const CS203_redirect cr{datain, testout}; // 重定向输入,输出 + main(); + // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向 + } + CHECK(compareFiles(testout, dataout)); + } +} +} +#endif //CS203_DSAA_TEST_MACRO \ No newline at end of file diff --git a/algorithm/2020S/20200511/20200511_fst.cpp b/algorithm/2020S/20200511/20200511_fst.cpp new file mode 100644 index 00000000..b9d592f9 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_fst.cpp @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ + +#include +#include +#include +#include + +#ifdef CS203_DSAA_TEST_MACRO +namespace fst_20200511{ +#endif + +using std::cin; +using std::cout; +using std::string; +using std::vector; +static constexpr const char end{'\n'}; + +using num_t = int32_t; + +int main() { + int32_t c_n{0}, command{0}; + string init, str; + int32_t size_1; + cin >> c_n; + std::deque deq_s; + for (int32_t i{0}; i < c_n; ++i) { + cin >> command; + switch (command) { + case 1: { + cin >> str; + deq_s.push_back(init); + init += str; + break; + } + case 2: { + cin >> size_1; + deq_s.push_back(init); + init = init.substr(0, init.size() - size_1); + break; + } + case 3: { + cin >> size_1; + cout << init[size_1 - 1] << end; + break; + } + case 4: { + init = deq_s.back(); + deq_s.pop_back(); + break; + } + default: { + return -1; + } + } + } + cout.flush(); + return 0; +} + +static const auto faster_streams = [] { + srand(time(nullptr)); + // use time to init the random seed + std::ios::sync_with_stdio(false); + std::istream::sync_with_stdio(false); + std::ostream::sync_with_stdio(false); + std::cin.tie(nullptr); + std::cout.tie(nullptr); + // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能. + return 0; +}(); +#ifdef CS203_DSAA_TEST_MACRO +} +#endif diff --git a/algorithm/2020S/20200511/20200511_fst_data/1.data.in b/algorithm/2020S/20200511/20200511_fst_data/1.data.in new file mode 100644 index 00000000..2def641f --- /dev/null +++ b/algorithm/2020S/20200511/20200511_fst_data/1.data.in @@ -0,0 +1,9 @@ +8 +1 abc +3 3 +2 3 +1 xy +3 2 +4 +4 +3 1 \ No newline at end of file diff --git a/algorithm/2020S/20200511/20200511_fst_data/1.data.out b/algorithm/2020S/20200511/20200511_fst_data/1.data.out new file mode 100644 index 00000000..ec4f2596 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_fst_data/1.data.out @@ -0,0 +1,3 @@ +c +y +a diff --git a/algorithm/2020S/20200511/20200511_fst_test.cpp b/algorithm/2020S/20200511/20200511_fst_test.cpp new file mode 100644 index 00000000..2a99488d --- /dev/null +++ b/algorithm/2020S/20200511/20200511_fst_test.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +#ifdef CS203_DSAA_TEST_MACRO + +#include +#include +#include +#include + +#include "20200511_fst.cpp" + +std::string getFilePath() noexcept { + return "./../../../../algorithm/2020S/20200511/20200511_fst_data/"; +} + +const std::string CS203_redirect::file_paths = getFilePath(); + +namespace fst_20200511 { +using std::tie; +using std::cin; +using std::cout; +using std::tuple; +using std::vector; + +TEST_CASE("test case with sequence", "[test 02 A]") { + CS203_sequence sequence{1, 1, 0}; // // 基础设定,[1,1] + sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in + sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out + sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out + const auto files_name = sequence.get_files(true); + // 获取一个std::tuple , + // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名. + for (const auto &file_name: files_name) { + const auto &[datain, dataout, testout] = file_name; + { + const CS203_redirect cr{datain, testout}; // 重定向输入,输出 + main(); + // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向 + } + CHECK(compareFiles(testout, dataout)); + } +} +} +#endif //CS203_DSAA_TEST_MACRO \ No newline at end of file diff --git a/algorithm/2020S/20200511/20200511_snd.cpp b/algorithm/2020S/20200511/20200511_snd.cpp new file mode 100644 index 00000000..4d7add62 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_snd.cpp @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ + +#include +#include +#include +#include +#include + +#ifdef CS203_DSAA_TEST_MACRO +namespace snd_20200511 { +#endif + +using std::cin; +using std::cout; +using std::string; +using std::vector; +using std::unordered_map; +static constexpr const char end{'\n'}; +static constexpr const int32_t yushu{835672545}; +using num_t = int32_t; + +int main() { + string ari; + int32_t dir_size{0}; + cin >> ari; + cin >> dir_size; + vector dir(dir_size); + for (auto &i: dir) { + cin >> i; + } + + const int32_t ari_size = ari.size(); + std::sort(std::begin(dir), std::end(dir), + [](const string &str1, const string &str2) { return str1.size() < str2.size(); }); + int32_t will_return{0}; + unordered_map now; + unordered_map next; + now.insert(std::make_pair(0, 1)); + while (!now.empty()) { + for (const auto &i: now) { + if (i.first == ari_size) { + will_return += i.second; + will_return %= yushu; + } + const string &temp_str(ari.substr(i.first)); + for (const auto &j: dir) { + if (temp_str.size() < j.size()) { + break; + } + if (temp_str.substr(0, j.size()) == j) { + next[i.first + j.size()] += now[i.first]; + } + } + } + now = std::move(next); + next = unordered_map(); + will_return %= yushu; + } + cout << will_return << end; + cout.flush(); + return 0; +} + +static const auto faster_streams = [] { + srand(time(nullptr)); + // use time to init the random seed + std::ios::sync_with_stdio(false); + std::istream::sync_with_stdio(false); + std::ostream::sync_with_stdio(false); + std::cin.tie(nullptr); + std::cout.tie(nullptr); + // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能. + return 0; +}(); +#ifdef CS203_DSAA_TEST_MACRO +} +#endif diff --git a/algorithm/2020S/20200511/20200511_snd_data/1.data.in b/algorithm/2020S/20200511/20200511_snd_data/1.data.in new file mode 100644 index 00000000..bd205c96 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_snd_data/1.data.in @@ -0,0 +1,7 @@ +abcba +5 +ab +cb +bc +ba +a \ No newline at end of file diff --git a/algorithm/2020S/20200511/20200511_snd_data/1.data.out b/algorithm/2020S/20200511/20200511_snd_data/1.data.out new file mode 100644 index 00000000..0cfbf088 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_snd_data/1.data.out @@ -0,0 +1 @@ +2 diff --git a/algorithm/2020S/20200511/20200511_snd_test.cpp b/algorithm/2020S/20200511/20200511_snd_test.cpp new file mode 100644 index 00000000..97a8f39f --- /dev/null +++ b/algorithm/2020S/20200511/20200511_snd_test.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +#ifdef CS203_DSAA_TEST_MACRO + +#include +#include +#include +#include + +#include "20200511_snd.cpp" + +std::string getFilePath() noexcept { + return "./../../../../algorithm/2020S/20200511/20200511_snd_data/"; +} + +const std::string CS203_redirect::file_paths = getFilePath(); + +namespace snd_20200511 { +using std::tie; +using std::cin; +using std::cout; +using std::tuple; +using std::vector; + +TEST_CASE("test case with sequence", "[test snd_20200511 1]") { + CS203_sequence sequence{1, 1, 0}; // // 基础设定,[1,1] + sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in + sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out + sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out + const auto files_name = sequence.get_files(true); + // 获取一个std::tuple , + // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名. + for (const auto &file_name: files_name) { + const auto &[datain, dataout, testout] = file_name; + { + const CS203_redirect cr{datain, testout}; // 重定向输入,输出 + main(); + // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向 + } + CHECK(compareFiles(testout, dataout)); + } +} +} +#endif //CS203_DSAA_TEST_MACRO \ No newline at end of file diff --git a/algorithm/2020S/20200511/20200511_trd.cpp b/algorithm/2020S/20200511/20200511_trd.cpp new file mode 100644 index 00000000..6c17b2e0 --- /dev/null +++ b/algorithm/2020S/20200511/20200511_trd.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ + +#include +#include +#include + +#ifdef CS203_DSAA_TEST_MACRO +namespace trd_20200511{ +#endif + +using std::cin; +using std::cout; +using std::string; +using std::vector; +static constexpr const char end{'\n'}; + +using num_t = int32_t; + +int main() { + // it just read input + int32_t n{0}, m{0}; + cin >> n >> m; + vector c(n), a(m), b(m); + for (auto &i: c) { + cin >> i; + } + for (int32_t j{0}; j < m; ++j) { + cin >> a[j] >> b[j]; + } + return 0; +} + +static const auto faster_streams = [] { + srand(time(nullptr)); + // use time to init the random seed + std::ios::sync_with_stdio(false); + std::istream::sync_with_stdio(false); + std::ostream::sync_with_stdio(false); + std::cin.tie(nullptr); + std::cout.tie(nullptr); + // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能. + return 0; +}(); +#ifdef CS203_DSAA_TEST_MACRO +} +#endif diff --git a/algorithm/2020S/20200511/20200511_trd_data/1.data.in b/algorithm/2020S/20200511/20200511_trd_data/1.data.in new file mode 100644 index 00000000..8a86fd8c --- /dev/null +++ b/algorithm/2020S/20200511/20200511_trd_data/1.data.in @@ -0,0 +1,7 @@ +abacaba +5 +2 1 4 +1 4 b +1 5 b +2 4 6 +2 1 7 diff --git a/algorithm/2020S/20200511/20200511_trd_data/1.data.out b/algorithm/2020S/20200511/20200511_trd_data/1.data.out new file mode 100644 index 00000000..e69de29b diff --git a/algorithm/2020S/20200511/20200511_trd_test.cpp b/algorithm/2020S/20200511/20200511_trd_test.cpp new file mode 100644 index 00000000..ef0e4f3e --- /dev/null +++ b/algorithm/2020S/20200511/20200511_trd_test.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +#ifdef CS203_DSAA_TEST_MACRO + +#include +#include +#include +#include + +#include "20200511_trd.cpp" + +std::string getFilePath() noexcept { + return "./../../../../algorithm/2020S/20200511/20200511_trd_data/"; +} + +const std::string CS203_redirect::file_paths = getFilePath(); + +namespace trd_20200511 { +using std::tie; +using std::cin; +using std::cout; +using std::tuple; +using std::vector; + +TEST_CASE("test case with sequence", "[test trd_20200511 1]") { + CS203_sequence sequence{1, 1, 0}; // // 基础设定,[1,1] + sequence.set_postfix_of_datain("data.in"); // 输入数据后缀,默认为 data.in + sequence.set_postfix_of_dataout("data.out"); // except输出数据后缀,默认为 data.out + sequence.set_postfix_of_testout("test.out"); // 测试输出数据后缀,默认为 test.out + const auto files_name = sequence.get_files(true); + // 获取一个std::tuple , + // 其中每个tuple内为 `输入数据`,`except输出数据`,`测试输出数据`名. + for (const auto &file_name: files_name) { + const auto &[datain, dataout, testout] = file_name; + { + const CS203_redirect cr{datain, testout}; // 重定向输入,输出 + main(); + // 用括号括住是为了让CS203_redirect在这里被析构,停止重定向 + } + CHECK(compareFiles(testout, dataout)); + } +} +} +#endif //CS203_DSAA_TEST_MACRO \ No newline at end of file diff --git a/algorithm/2020S/20200511/CMakeLists.txt b/algorithm/2020S/20200511/CMakeLists.txt new file mode 100644 index 00000000..fbd7160a --- /dev/null +++ b/algorithm/2020S/20200511/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.16.6) +set(PROJECT_DAY 20200511) +project(${PROJECT_NAME}_${PROJECT_DAY}) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + +enable_testing() +set(dependencies "fst" "snd" "trd" "4th") +foreach (elementName IN LISTS dependencies) + add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_DAY}_${elementName}.cpp) + set(CMAKE_CXX_STANDARD 17) + add_executable(${PROJECT_NAME}_${elementName}_test ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_DAY}_${elementName}_test.cpp) + target_compile_definitions(${PROJECT_NAME}_${elementName}_test PRIVATE CS203_DSAA_TEST_MACRO) + target_link_libraries(${PROJECT_NAME}_${elementName}_test PRIVATE CS203_DSAA_template_INCLUDE) + MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_DAY}_${elementName}.cpp") + add_test(${PROJECT_NAME}_${elementName}_CTEST ${PROJECT_NAME}_${elementName}_test) + set(CMAKE_CXX_STANDARD 11) +endforeach () +unset(dependencies) diff --git a/algorithm/2020S/20200511/README.md b/algorithm/2020S/20200511/README.md new file mode 100644 index 00000000..37f1bf76 --- /dev/null +++ b/algorithm/2020S/20200511/README.md @@ -0,0 +1,4 @@ +20200511 code test + +company: [encryet company name](./company.gpg) + diff --git a/algorithm/2020S/20200511/company.gpg b/algorithm/2020S/20200511/company.gpg new file mode 100644 index 00000000..de5f0bd4 Binary files /dev/null and b/algorithm/2020S/20200511/company.gpg differ diff --git a/algorithm/2020S/20200610/20200610_fst.cpp b/algorithm/2020S/20200610/20200610_fst.cpp new file mode 100644 index 00000000..0bc3b0ae --- /dev/null +++ b/algorithm/2020S/20200610/20200610_fst.cpp @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ + +#include +#include +#include +#include +#include + +#ifdef CS203_DSAA_TEST_MACRO +namespace fst_20200610 { +#endif + +using std::cin; +using std::cout; +using std::string; +using std::vector; +using std::unordered_set; +static constexpr const char end{'\n'}; + +using num_t = int32_t; + +int main() { + int32_t m, n; + cin >> m >> n; + if (m < 0 || n < 0 || m > n || (m == 0 && n == 0)) { + return 0; + } + if (m == 1 & n == 1 || (m == 0 && n == 1)) { + return 9; + } + vector> umap_next = {{0}, + {2, 4, 5, 6, 8}, + {1, 3, 4, 5, 6, 7, 9}, + {2, 4, 5, 6, 8}, + {1, 2, 3, 5, 7, 8, 9}, + {1, 2, 3, 4, 6, 7, 8, 9}, + {1, 2, 3, 5, 7, 8, 9}, + {2, 4, 5, 6, 8}, + {1, 3, 4, 5, 6, 7, 9}, + {2, 4, 5, 6, 8} + }; + vector> umap_next_2 = { + {0}, + {2, 4, 5}, + {5}, + {2, 5, 6}, + {5}, + {}, + {5}, + {4, 5, 8}, + {5}, + {5, 6, 8} + }; + vector> sequence; + for (int i = 1; i <= 9; ++i) { + sequence.push_back({i}); + } + int result = 0; + for (int j = 1; j < n; ++j) { + if (j >= m) { + result += sequence.size(); + } + vector> temp; + for (const auto &i: sequence) { + int32_t last_one = i.back(); + unordered_set can_add(umap_next[last_one]); + for (size_t k = 0; k < i.size() - 1; ++k) { + if (umap_next[last_one].count(i[k])) { + can_add.erase(i[k]); + } + } + if (i.size() == 2 && i[0] == 4 && i[1] == 7) { + const int useless = 2; + } + for (size_t k = 0; last_one != 5 && k < i.size() - 1; ++k) { + const int middle_value = 2 * i[k] - last_one; + if (umap_next_2[last_one].find(i[k]) != umap_next_2[last_one].end() + && std::find(i.begin(), i.end(), middle_value) == i.end() + && middle_value >= 1 && middle_value <= 9) { + can_add.insert(middle_value); + } + } + for (const auto &k: can_add) { + vector middle(i); + middle.push_back(k); + temp.push_back(middle); + } + } + sequence = temp; + } + result += sequence.size(); + std::cout << result << end; + return 0; +} + +static const auto faster_streams = [] { + srand(time(nullptr)); + // use time to init the random seed + std::ios::sync_with_stdio(false); + std::istream::sync_with_stdio(false); + std::ostream::sync_with_stdio(false); + std::cin.tie(nullptr); + std::cout.tie(nullptr); + // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能. + return 0; +}(); +#ifdef CS203_DSAA_TEST_MACRO +} +#endif diff --git a/algorithm/2020S/20200610/20200610_snd.cpp b/algorithm/2020S/20200610/20200610_snd.cpp new file mode 100644 index 00000000..df786fda --- /dev/null +++ b/algorithm/2020S/20200610/20200610_snd.cpp @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ + +#include +#include +#include + +#ifdef CS203_DSAA_TEST_MACRO +namespace snd_20200610 { +#endif + +using std::cin; +using std::cout; +using std::vector; +static constexpr const char end{'\n'}; + +using num_t = int32_t; + +int main() { + int32_t n{0}, will_return{0}; + cin >> n; + vector results; + bool can{true}; + while (n > 1 && can) { + can = false; + for (int i = 9; i >= 2; --i) { + if (n % i == 0) { + results.push_back(i); + n = n / i; + can = true; + break; + } + } + } + if (n > 9) { + cout << -1 << end; + return 0; + } + std::sort(results.begin(), results.end()); + for (const auto &x: results) { + will_return *= 10; + will_return += x; + } + cout << will_return << end; + return 0; +} + +static const auto faster_streams = [] { + srand(time(nullptr)); + // use time to init the random seed + std::ios::sync_with_stdio(false); + std::istream::sync_with_stdio(false); + std::ostream::sync_with_stdio(false); + std::cin.tie(nullptr); + std::cout.tie(nullptr); + // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能. + return 0; +}(); +#ifdef CS203_DSAA_TEST_MACRO +} +#endif diff --git a/algorithm/2020S/20200610/20200610_trd.cpp b/algorithm/2020S/20200610/20200610_trd.cpp new file mode 100644 index 00000000..0923b994 --- /dev/null +++ b/algorithm/2020S/20200610/20200610_trd.cpp @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later +/* CS203_DSAA_template + Copyright (C) 2020-2022 nanoseeds +*/ +#include +#include +#include + +#ifdef CS203_DSAA_TEST_MACRO +namespace trd_20200610 { +#endif + +using std::cin; +using std::cout; +using std::vector; +static constexpr const char end{'\n'}; + +using num_t = int32_t; + +int main() { + int32_t temp{0}, result{0}, n{0}; + std::cin >> n; + for (int i = 1; i < n; ++i) { + if (i * (i + 1) >= 2 * n) { + temp = i - 1; + break; + } + } + for (int j = 1; j <= temp; ++j) { + result += j * j; + } + result += (n - temp * (temp + 1) / 2) * (temp + 1); + std::cout << result << end; + return 0; +} + +static const auto faster_streams = [] { + srand(time(nullptr)); + // use time to init the random seed + std::ios::sync_with_stdio(false); + std::istream::sync_with_stdio(false); + std::ostream::sync_with_stdio(false); + std::cin.tie(nullptr); + std::cout.tie(nullptr); + // 关闭c++风格输入输出 , 与C风格输入输出的同步,提高性能. + return 0; +}(); +#ifdef CS203_DSAA_TEST_MACRO +} +#endif diff --git a/algorithm/2020S/20200610/CMakeLists.txt b/algorithm/2020S/20200610/CMakeLists.txt new file mode 100644 index 00000000..2ea34ec5 --- /dev/null +++ b/algorithm/2020S/20200610/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.16.6) +set(PROJECT_DAY 20200610) +project(${PROJECT_NAME}_${PROJECT_DAY}) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + +set(dependencies "fst" "snd" "trd") +foreach (elementName IN LISTS dependencies) + add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_DAY}_${elementName}.cpp) +endforeach () +unset(dependencies) diff --git a/algorithm/2020S/20200610/README.md b/algorithm/2020S/20200610/README.md new file mode 100644 index 00000000..ae05fc7c --- /dev/null +++ b/algorithm/2020S/20200610/README.md @@ -0,0 +1,4 @@ +20200610 code test + +company: [encryet company name](./company.gpg) + diff --git a/algorithm/2020S/20200610/company.gpg b/algorithm/2020S/20200610/company.gpg new file mode 100644 index 00000000..9c8e5156 Binary files /dev/null and b/algorithm/2020S/20200610/company.gpg differ diff --git a/algorithm/2020S/CMakeLists.txt b/algorithm/2020S/CMakeLists.txt new file mode 100644 index 00000000..e9573096 --- /dev/null +++ b/algorithm/2020S/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.16.6) +set(YEAR 2020) +set(SEMESTER SPRING) + +project(CS203_DSAA_${YEAR}_${SEMESTER} LANGUAGES CXX) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + +enable_testing() +file(GLOB USER_LIBS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/2020*) # regex to find libs, more flexibility +foreach (elementName IN LISTS USER_LIBS_PATH) + add_subdirectory(${elementName}) +endforeach () +unset(USER_LIBS_PATH) diff --git a/algorithm/CMakeLists.txt b/algorithm/CMakeLists.txt index 1c1c79cd..a5befecd 100644 --- a/algorithm/CMakeLists.txt +++ b/algorithm/CMakeLists.txt @@ -10,6 +10,7 @@ set(dependencies dp fft sort tree binary_search string_search graph moderncpp) LIST(APPEND dependencies 2021F stack array list effective_cpp) LIST(APPEND dependencies string trie math disjoint_set divide_merge) LIST(APPEND dependencies matrix cs302 queue associative_container) +LIST(APPEND dependencies 2020S) foreach (elementName IN LISTS dependencies) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${elementName}) endforeach () diff --git a/algorithm/array/CMakeLists.txt b/algorithm/array/CMakeLists.txt index 3e806fd8..afbcf41c 100644 --- a/algorithm/array/CMakeLists.txt +++ b/algorithm/array/CMakeLists.txt @@ -20,7 +20,8 @@ list(APPEND leetcode_order 334 238 560 804 806) list(APPEND leetcode_order 807 811 830 832 840) list(APPEND leetcode_order 849 852 867 868 896) list(APPEND leetcode_order 905 908 922 941 942) -list(APPEND leetcode_order 944 977) +list(APPEND leetcode_order 944 977 985 986 989) +list(APPEND leetcode_order 999 1010 1013 1030) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/array/leetcode_1010.cpp b/algorithm/array/leetcode_1010.cpp new file mode 100644 index 00000000..e91a6d9e --- /dev/null +++ b/algorithm/array/leetcode_1010.cpp @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_1010_test.hpp" +#include + +namespace leetcode_1010 { +using std::array; + +int32_t leetcode_1010::numPairsDivisibleBy60(const vector &time) { + array uses{0}; + int32_t willreturn{0}; + for (const int32_t i: time) { + uses[i % 60]++; + willreturn += uses[(60 - i % 60) % 60]; + } + return willreturn - uses[30] - uses[0]; +} + +} diff --git a/algorithm/array/leetcode_1010_test.hpp b/algorithm/array/leetcode_1010_test.hpp new file mode 100644 index 00000000..0c122a8e --- /dev/null +++ b/algorithm/array/leetcode_1010_test.hpp @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag array +//@Tag 数组 +//@Tag 暴力解法 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1010_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1010_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_1010 { +using std::vector; + +struct leetcode_1010 { + static int32_t numPairsDivisibleBy60(const vector &time); +}; + +TEST_CASE("test case 1 {test_1010}", "{test_1010}") { + const vector input{30, 20, 150, 100, 40}; + static constexpr const auto result{3}; + CHECK(result == leetcode_1010::numPairsDivisibleBy60(input)); +} + +TEST_CASE("test case 2 {test_1010}", "{test_1010}") { + const vector input{60, 60, 60}; + static constexpr const auto result{3}; + CHECK(result == leetcode_1010::numPairsDivisibleBy60(input)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1010_TEST_HPP diff --git a/algorithm/array/leetcode_1013.cpp b/algorithm/array/leetcode_1013.cpp new file mode 100644 index 00000000..7834ddc0 --- /dev/null +++ b/algorithm/array/leetcode_1013.cpp @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_1013_test.hpp" +#include + +namespace leetcode_1013 { + +bool leetcode_1013::canThreePartsEqualSum(const vector &arr) { + const int32_t sums = std::accumulate(arr.begin(), arr.end(), 0); + if (sums % 3 != 0) { + return false; + } + const int32_t divide = sums / 3; + for (int x{1}, y = arr.size() - 2, sum_x{arr.front()}, sum_y{arr.back()}; x < y;) { + if (sum_x != divide) { + sum_x += arr[x]; + x++; + } + if (sum_y != divide) { + sum_y += arr[y]; + y--; + } + if (sum_x == divide && sum_y == divide && x <= y) { + return true; + } + } + return false; +} +} diff --git a/algorithm/array/leetcode_1013_test.hpp b/algorithm/array/leetcode_1013_test.hpp new file mode 100644 index 00000000..039e8e08 --- /dev/null +++ b/algorithm/array/leetcode_1013_test.hpp @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag array +//@Tag 数组 +//@Tag "双"指针 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1013_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1013_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_1013 { +using std::vector; + +struct leetcode_1013 { + static bool canThreePartsEqualSum(const vector &arr); +}; + +TEST_CASE("test case 1 {test_1013}", "{test_1013}") { + const vector input{1, -1, 1, -1}; + CHECK_FALSE(leetcode_1013::canThreePartsEqualSum(input)); +} + +TEST_CASE("test case 5 {test_1013}", "{test_1013}") { + const vector input{0, 1, -1, 1, -1, 0}; + CHECK(leetcode_1013::canThreePartsEqualSum(input)); +} + +TEST_CASE("test case 2 {test_1013}", "{test_1013}") { + const vector input{3, 3, 6, 5, -2, 2, 5, 1, -9, 4}; + CHECK(leetcode_1013::canThreePartsEqualSum(input)); +} + +TEST_CASE("test case 3 {test_1013}", "{test_1013}") { + const vector input{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1}; + CHECK(leetcode_1013::canThreePartsEqualSum(input)); +} + +TEST_CASE("test case 4 {test_1013}", "{test_1013}") { + const vector input{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1}; + CHECK_FALSE(leetcode_1013::canThreePartsEqualSum(input)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1013_TEST_HPP diff --git a/algorithm/array/leetcode_1030.cpp b/algorithm/array/leetcode_1030.cpp new file mode 100644 index 00000000..b619bf8c --- /dev/null +++ b/algorithm/array/leetcode_1030.cpp @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_1030_test.hpp" + +namespace leetcode_1030 { + +vector> +leetcode_1030::allCellsDistOrder(int32_t rows, int32_t cols, int32_t rCenter, int32_t cCenter) { + const auto distance = [rCenter, cCenter](int32_t x, int32_t y) { + return std::abs(rCenter - x) + std::abs(cCenter - y); + }; + const auto max_distance = std::max( + {distance(0, 0), distance(0, cols - 1), distance(rows - 1, 0), distance(rows - 1, cols - 1)} + ); + vector> will_return{{rCenter, cCenter}}; + const auto in = [rows, cols, &will_return](int32_t x, int32_t y) { + const auto judge = (0 <= x && x < rows) && (0 <= y && y < cols); + if (judge) { + will_return.push_back({x, y}); + } + }; + for (int32_t i{1}; i <= max_distance; ++i) { + in(rCenter, cCenter + i); + in(rCenter, cCenter - i); + for (int32_t j{1}; j < i; ++j) { + const auto row{j}, col{i - j}; + in(rCenter + row, cCenter + col); + in(rCenter + row, cCenter - col); + in(rCenter - row, cCenter + col); + in(rCenter - row, cCenter - col); + } + in(rCenter + i, cCenter); + in(rCenter - i, cCenter); + } + return will_return; +} + +vector> +leetcode_1030::allCellsDistOrderNLogN(int32_t rows, int32_t cols, int32_t rCenter, int32_t cCenter) { + struct point final { + int32_t x{0}, y{0}; + + point(int32_t x, int32_t y) : x(x), y(y) {}; + + bool operator<(const point &p) const { + return abs(x) + abs(y) < abs(p.x) + abs(p.y); + } + }; + vector pois; + vector> willreturn; + for (int32_t i{0}; i < rows; i++) { + for (int32_t j{0}; j < cols; j++) { + pois.emplace_back(i - rCenter, j - cCenter); + } + } + sort(pois.begin(), pois.end(), std::less()); + for (size_t i{0}; i < pois.size(); i++) { + willreturn.emplace_back(vector{pois[i].x + rCenter, pois[i].y + cCenter}); + } + return willreturn; +} + +} diff --git a/algorithm/array/leetcode_1030_test.hpp b/algorithm/array/leetcode_1030_test.hpp new file mode 100644 index 00000000..b7282797 --- /dev/null +++ b/algorithm/array/leetcode_1030_test.hpp @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag array +//@Tag 数组 +//@Tag 排序 +//@Tag 模拟 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1030_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1030_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_1030 { +using std::vector; + +struct leetcode_1030 { + static vector> allCellsDistOrder(int32_t rows, int32_t cols, int32_t rCenter, int32_t cCenter); + + static vector> allCellsDistOrderNLogN(int32_t rows, int32_t cols, int32_t rCenter, int32_t cCenter); +}; + +bool check(const vector> &input, int32_t rCenter, int32_t cCenter) { + vector test{}; + for (const auto &point: input) { + test.push_back(std::abs(rCenter - point[0]) + std::abs(cCenter - point[1])); + } + return std::is_sorted(test.cbegin(), test.cend()); +} + +TEST_CASE("test case 3-2 {test_1030}", "{test_1030}") { + static constexpr const auto rows{2}, cols{3}, rCenter{1}, cCenter{2}; + const vector> result{leetcode_1030::allCellsDistOrder(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} + +TEST_CASE("test case 3-1 {test_1030}", "{test_1030}") { + static constexpr const auto rows{2}, cols{3}, rCenter{1}, cCenter{2}; + const vector> result{leetcode_1030::allCellsDistOrderNLogN(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} + +TEST_CASE("test case 2-2 {test_1030}", "{test_1030}") { + static constexpr const auto rows{2}, cols{2}, rCenter{0}, cCenter{1}; + const vector> result{leetcode_1030::allCellsDistOrder(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} + +TEST_CASE("test case 2-1 {test_1030}", "{test_1030}") { + static constexpr const auto rows{2}, cols{2}, rCenter{0}, cCenter{1}; + const vector> result{leetcode_1030::allCellsDistOrderNLogN(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} + +TEST_CASE("test case 1-2 {test_1030}", "{test_1030}") { + static constexpr const auto rows{1}, cols{2}, rCenter{0}, cCenter{0}; + const vector> result{leetcode_1030::allCellsDistOrder(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} + +TEST_CASE("test case 1-1 {test_1030}", "{test_1030}") { + static constexpr const auto rows{1}, cols{2}, rCenter{0}, cCenter{0}; + const vector> result{leetcode_1030::allCellsDistOrderNLogN(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} + +TEST_CASE("test case 4-2 {test_1030}", "{test_1030}") { + static constexpr const auto rows{11}, cols{45}, rCenter{1}, cCenter{4}; + const vector> result{leetcode_1030::allCellsDistOrder(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} + +TEST_CASE("test case 4-1 {test_1030}", "{test_1030}") { + static constexpr const auto rows{11}, cols{45}, rCenter{1}, cCenter{4}; + const vector> result{leetcode_1030::allCellsDistOrderNLogN(rows, cols, rCenter, cCenter)}; + CHECK(check(result, rCenter, cCenter)); + CHECK(result.size() == rows * cols); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1030_TEST_HPP diff --git a/algorithm/array/leetcode_977_test.hpp b/algorithm/array/leetcode_977_test.hpp index f39fe1ae..673c41e9 100644 --- a/algorithm/array/leetcode_977_test.hpp +++ b/algorithm/array/leetcode_977_test.hpp @@ -14,11 +14,9 @@ Copyright (C) 2022 nanoseeds #include #include #include -#include namespace leetcode_977 { using std::vector; -using std::string; struct leetcode_977 { static vector sortedSquares(const vector &nums); diff --git a/algorithm/array/leetcode_985.cpp b/algorithm/array/leetcode_985.cpp new file mode 100644 index 00000000..860efbe2 --- /dev/null +++ b/algorithm/array/leetcode_985.cpp @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_985_test.hpp" + +namespace leetcode_985 { + +vector leetcode_985::sumEvenAfterQueries(const vector &nums, const vector> &queries) { + vector willreturn; + vector mut_nums{nums}; + willreturn.reserve(queries.size()); + int32_t sums{0}; + for (const auto num: mut_nums) { + sums += ((num % 2 == 0) ? num : 0); + } + for (const auto &query: queries) { + //for (int32_t i{0}; i < queries.size(); i++) { + const auto val{query[0]}; + const auto index{query[1]}; + const auto lastIdx{mut_nums[index]}; + if (lastIdx % 2 == 0) { + if (val % 2 == 0) { + sums += val; + } else { + sums -= lastIdx; + } + } else if (val % 2 != 0) { + sums += (lastIdx + val); + } + mut_nums[index] += val; + willreturn.push_back(sums); + } + return willreturn; +} + +} diff --git a/algorithm/array/leetcode_985_test.hpp b/algorithm/array/leetcode_985_test.hpp new file mode 100644 index 00000000..1fa5c365 --- /dev/null +++ b/algorithm/array/leetcode_985_test.hpp @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag array +//@Tag +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_985_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_985_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_985 { +using std::vector; + +struct leetcode_985 { + static vector sumEvenAfterQueries(const vector &nums, const vector> &queries); +}; + + +using Catch::Matchers::Equals; + +TEST_CASE("test case 1 {test_985}", "{test_985}") { + const vector input{1, 2, 3, 4}; + const vector> queries{ + {1, 0}, + {-3, 1}, + {-4, 0}, + {2, 3}, + }; + const vector result{8, 6, 2, 4}; + CHECK_THAT(result, Equals(leetcode_985::sumEvenAfterQueries(input, queries))); +} + +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_985_TEST_HPP diff --git a/algorithm/array/leetcode_986.cpp b/algorithm/array/leetcode_986.cpp new file mode 100644 index 00000000..070b3d1b --- /dev/null +++ b/algorithm/array/leetcode_986.cpp @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_986_test.hpp" + +namespace leetcode_986 { + +vector> leetcode_986::intervalIntersection(const vector> &fst, const vector> &snd) { + vector> will_return; + const auto fst_size{fst.size()}, snd_size{snd.size()}; + const auto judgement = [&fst, &snd](size_t pre, size_t post) -> vector { + const auto &fst_element{fst[pre]}, &snd_element{snd[post]}; + return {std::max(fst_element[0], snd_element[0]), + std::min(fst_element[1], snd_element[1])}; + }; + for (size_t x{0}, y{0}, judge{0}; x < fst_size && y < snd_size;) { + if (fst[x][1] < snd[y][0]) { + ++x; + } else if (fst[x][0] > snd[y][1]) { + ++y; + } else { + const vector temp = judgement(x, y); + will_return.emplace_back(temp); + judge = ((temp[1] >= fst[x][1]) == 1 ? 1 : 0); + x += judge; + y += (1 - judge); + } + } + return will_return; +} +} diff --git a/algorithm/array/leetcode_986_test.hpp b/algorithm/array/leetcode_986_test.hpp new file mode 100644 index 00000000..06001053 --- /dev/null +++ b/algorithm/array/leetcode_986_test.hpp @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag array +//@Tag 双指针 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_986_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_986_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_986 { +using std::vector; + +struct leetcode_986 { + static vector> intervalIntersection(const vector> &fst, const vector> &snd); +}; + + +using Catch::Matchers::Equals; + +TEST_CASE("test case 1 {test_986}", "{test_986}") { + const vector> fst{{0, 2}, + {5, 10}, + {13, 23}, + {24, 25}}; + const vector> snd{{1, 5}, + {8, 12}, + {15, 24}, + {25, 26}}; + const vector> result{{1, 2}, + {5, 5}, + {8, 10}, + {15, 23}, + {24, 24}, + {25, 25}}; + CHECK_THAT(result, Equals(leetcode_986::intervalIntersection(fst, snd))); +} + +TEST_CASE("test case 3 {test_986}", "{test_986}") { + const vector> fst{{17, 20}}; + const vector> snd{{2, 3}, + {6, 8}, + {12, 14}, + {19, 20}}; + const vector> result{{19, 20}}; + CHECK_THAT(result, Equals(leetcode_986::intervalIntersection(fst, snd))); +} + +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_986_TEST_HPP diff --git a/algorithm/array/leetcode_989.cpp b/algorithm/array/leetcode_989.cpp new file mode 100644 index 00000000..9d13a3b9 --- /dev/null +++ b/algorithm/array/leetcode_989.cpp @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_989_test.hpp" + +namespace leetcode_989 { + +vector leetcode_989::addToArrayForm(const vector &num, int32_t k) { + vector will_return{num}; + const auto num_size{num.size()}; + int32_t last{0}; + for (size_t i{will_return.size()}; i >= 1; --i) { + last += k % 10; + k = k / 10; + will_return[i-1] += last; + last = (will_return[i - 1] >= 10); + will_return[i-1] = will_return[i-1] % 10; + } + while (k > 0 || last > 0) { + last += k % 10; + k /= 10; + will_return.emplace(will_return.begin(), last % 10); + last = last / 10; + } + return will_return; +} +} diff --git a/algorithm/array/leetcode_989_test.hpp b/algorithm/array/leetcode_989_test.hpp new file mode 100644 index 00000000..ae7045c1 --- /dev/null +++ b/algorithm/array/leetcode_989_test.hpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag array +//@Tag 双指针 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_989_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_989_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_989 { +using std::vector; + +struct leetcode_989 { + static vector addToArrayForm(const vector &num, int32_t k); +}; + +using Catch::Matchers::Equals; + +TEST_CASE("test case 1 {test_989}", "{test_989}") { + const vector fst{1, 3, 2, 1}; + static constexpr const auto k{34}; + const vector result{1, 3, 5, 5}; + CHECK_THAT(result, Equals(leetcode_989::addToArrayForm(fst, k))); +} + +TEST_CASE("test case 2 {test_989}", "{test_989}") { + const vector fst{2, 7, 4}; + static constexpr const auto k{181}; + const vector result{4, 5, 5}; + CHECK_THAT(result, Equals(leetcode_989::addToArrayForm(fst, k))); +} + +TEST_CASE("test case 3 {test_989}", "{test_989}") { + const vector fst{2, 1, 5}; + static constexpr const auto k{806}; + const vector result{1, 0, 2, 1}; + CHECK_THAT(result, Equals(leetcode_989::addToArrayForm(fst, k))); +} + +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_989_TEST_HPP diff --git a/algorithm/array/leetcode_999.cpp b/algorithm/array/leetcode_999.cpp new file mode 100644 index 00000000..745b7924 --- /dev/null +++ b/algorithm/array/leetcode_999.cpp @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include +#include "leetcode_999_test.hpp" + +namespace leetcode_999 { +static constexpr const size_t board_size{8}; + +int32_t leetcode_999::numRookCaptures(const vector> &board) { + size_t x{0}, y{0}; + for (size_t i{0}; i < board_size; ++i) { + for (size_t j{0}; j < board_size; ++j) { + if (board[i][j] == 'R') { + x = i; + y = j; + } + } + } + std::array judge{false, false, false, false}; + for (size_t i{x + 1}; i < board_size; ++i) { + //cout << board[i][y] << endl; + switch (board[i][y]) { + case 'B': { + i = board_size; + break; + } + case '.': { + break; + } + case 'p': { + i = board_size; + judge[0] = true; + break; + } + + } + } + for (size_t i{x}; i >= 1; --i) { + //cout << board[i][y] << endl; + switch (board[i - 1][y]) { + case 'B': { + i = 1; + break; + } + + case '.': { + break; + } + + case 'p': { + judge[1] = true; + i = 1; + break; + } + } + } + + for (size_t i{y}; i >= 1; --i) { + switch (board[x][i - 1]) { + case 'B': { + i = 1; + break; + } + case '.': { + break; + } + case 'p': { + judge[2] = true; + i = 1; + break; + } + } + } + for (size_t i{y + 1}; i < board_size; i++) { + switch (board[x][i]) { + case 'B': { + i = board_size; + break; + } + case '.': { + break; + } + case 'p': { + judge[3] = true; + i = board_size; + break; + } + + } + } + return std::accumulate(judge.cbegin(), judge.cend(), 0); +} + +} diff --git a/algorithm/array/leetcode_999_test.hpp b/algorithm/array/leetcode_999_test.hpp new file mode 100644 index 00000000..ce981b71 --- /dev/null +++ b/algorithm/array/leetcode_999_test.hpp @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag array +//@Tag 双指针 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_999_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_999_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_999 { +using std::vector; + +struct leetcode_999 { + static int32_t numRookCaptures(const vector> &board); +}; + + +TEST_CASE("test case 1 {test_999}", "{test_999}") { + const vector> board{{'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', 'p', '.', '.', '.', '.'}, + {'.', '.', '.', 'R', '.', '.', '.', 'p'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', 'p', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}}; + static constexpr const auto result{3}; + CHECK(result == leetcode_999::numRookCaptures(board)); +} + +TEST_CASE("test case 2 {test_999}", "{test_999}") { + const vector> board{{'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', 'p', 'p', 'p', 'p', 'p', '.', '.'}, + {'.', 'p', 'p', 'B', 'p', 'p', '.', '.'}, + {'.', 'p', 'B', 'R', 'B', 'p', '.', '.'}, + {'.', 'p', 'p', 'B', 'p', 'p', '.', '.'}, + {'.', 'p', 'p', 'p', 'p', 'p', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}, + {'.', '.', '.', '.', '.', '.', '.', '.'}}; + static constexpr const auto result{0}; + CHECK(result == leetcode_999::numRookCaptures(board)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_999_TEST_HPP diff --git a/algorithm/associative_container/CMakeLists.txt b/algorithm/associative_container/CMakeLists.txt index c8d2d669..7e5e6213 100644 --- a/algorithm/associative_container/CMakeLists.txt +++ b/algorithm/associative_container/CMakeLists.txt @@ -8,7 +8,7 @@ enable_testing() set(dependencies) set(leetcode_order 888 890 893 1409 914) -list(APPEND leetcode_order 929 961) +list(APPEND leetcode_order 929 961 1002) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/associative_container/leetcode_1002.cpp b/algorithm/associative_container/leetcode_1002.cpp new file mode 100644 index 00000000..293a1fa7 --- /dev/null +++ b/algorithm/associative_container/leetcode_1002.cpp @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_1002_test.hpp" + +namespace leetcode_1002 { +static constexpr const size_t chars{26}; + +vector leetcode_1002::commonChars(const vector &words) { + std::array arr{0}; + std::fill(arr.begin(), arr.end(), 100); + for (const auto &word: words) { + std::array nums{0}; + for (const auto &ch: word) { + ++nums[ch - 'a']; + } + for (size_t i{0}; i < chars; ++i) { + arr[i] = std::min(nums[i], arr[i]); + } + } + vector will_return{}; + for (char i{'a'}; i <= 'z'; ++i) { + const auto ch = arr[i - 'a']; + for (size_t j{1}; j <= ch; j++) { + will_return.push_back(std::string{i}); + } + } + return will_return; +} + +} diff --git a/algorithm/associative_container/leetcode_1002_test.hpp b/algorithm/associative_container/leetcode_1002_test.hpp new file mode 100644 index 00000000..9aa8ab1f --- /dev/null +++ b/algorithm/associative_container/leetcode_1002_test.hpp @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag associative_container +//@Tag xor +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ASSOCIATIVE_CONTAINER_LEETCODE_1002_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_ASSOCIATIVE_CONTAINER_LEETCODE_1002_TEST_HPP + +#include +#include +#include +#include +#include +#include + +namespace leetcode_1002 { +using std::array; +using std::string; +using std::vector; + +struct leetcode_1002 { + static vector commonChars(const vector &words); +}; + +using Catch::Matchers::Equals; + +TEST_CASE("test case 1 {test_1002}", "{test_1002}") { + static constexpr const std::array arr{ + "bella", "label", "roller" + }; + static constexpr const std::array result{ + "e", "l", "l" + }; + CHECK_THAT((vector{result.cbegin(), result.cend()}), + Equals(leetcode_1002::commonChars({arr.cbegin(), arr.cend()})) + ); + +} + +TEST_CASE("test case 2 {test_1002}", "{test_1002}") { + static constexpr const std::array arr{ + "cool", "lock", "cook" + }; + static constexpr const std::array result{ + "c", "o" + }; + CHECK_THAT((vector{result.cbegin(), result.cend()}), + Equals(leetcode_1002::commonChars({arr.cbegin(), arr.cend()})) + ); + +} + +TEST_CASE("test case 3 {test_1002}", "{test_1002}") { + static constexpr const std::array arr{ + "acabcddd", "bcbdbcbd", "baddbadb", "cbdddcac", "aacbcccd", "ccccddda", "cababaab", "addcaccd" + }; + static constexpr const std::array result{}; + CHECK_THAT((vector{result.cbegin(), result.cend()}), + Equals(leetcode_1002::commonChars({arr.cbegin(), arr.cend()})) + ); + +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ASSOCIATIVE_CONTAINER_LEETCODE_1002_TEST_HPP diff --git a/algorithm/dp/CMakeLists.txt b/algorithm/dp/CMakeLists.txt index a5bdac37..b4edaf9a 100644 --- a/algorithm/dp/CMakeLists.txt +++ b/algorithm/dp/CMakeLists.txt @@ -17,7 +17,7 @@ LIST(APPEND leetcode_order 122 309 714 139 413) LIST(APPEND leetcode_order 91 264 931 304_1314 221) LIST(APPEND leetcode_order 516 300 174 376 643) LIST(APPEND leetcode_order 392 72 518 377 279) -LIST(APPEND leetcode_order so_46 435) +LIST(APPEND leetcode_order so_46 435 1025) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/dp/leetcode_1014.cpp b/algorithm/dp/leetcode_1014.cpp index 0f6448d3..10c5cb52 100644 --- a/algorithm/dp/leetcode_1014.cpp +++ b/algorithm/dp/leetcode_1014.cpp @@ -1,9 +1,3 @@ -/** - * @Github: https://github.com/Certseeds/CS203_DSAA_template - * @Author: nanos - * @Date: 2021-04-03 10:15:42 - * @LastEditors: nanos - */ // SPDX-License-Identifier: MIT /* CS203_DSAA_template diff --git a/algorithm/dp/leetcode_1025.cpp b/algorithm/dp/leetcode_1025.cpp new file mode 100644 index 00000000..099610ec --- /dev/null +++ b/algorithm/dp/leetcode_1025.cpp @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanos + +*/ +#include "leetcode_1025_test.hpp" + +namespace leetcode_1025 { + +bool leetcode_1025::divisorGame(int32_t n) { + vector status(n + 3, false); + status[1] = false; + status[2] = true; + for (size_t i{3}; i < n + 3; ++i) { + for (size_t j{1}; j < i; ++j) { + if (i % j == 0 && status[i - j] == false) { + status[i] = true; + break; + } + } + } + return status[n]; +} + +bool leetcode_1025::divisorGame2(int32_t n) { + return !(n & 1); +} + +} \ No newline at end of file diff --git a/algorithm/dp/leetcode_1025_test.hpp b/algorithm/dp/leetcode_1025_test.hpp new file mode 100644 index 00000000..ed881e63 --- /dev/null +++ b/algorithm/dp/leetcode_1025_test.hpp @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanos + +*/ +//@Tag DP +//@Tag 动态规划 +//@Tag 位运算 +//@Tag 数学归纳法 + +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_DP_LEETCODE_1025_TEST_CPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_DP_LEETCODE_1025_TEST_CPP + +#include +#include +#include +#include + +namespace leetcode_1025 { +using std::vector; + +struct leetcode_1025 { + static bool divisorGame(int32_t n); + + static bool divisorGame2(int32_t n); +}; + +TEST_CASE("1 [test_1025]", "[test_1025]") { + static constexpr const auto input{1}; + CHECK_FALSE(leetcode_1025::divisorGame(input)); + CHECK_FALSE(leetcode_1025::divisorGame2(input)); +} + +TEST_CASE("2 [test_1025]", "[test_1025]") { + static constexpr const auto input{2}; + CHECK(leetcode_1025::divisorGame(input)); + CHECK(leetcode_1025::divisorGame2(input)); +} + +TEST_CASE("3 [test_1025]", "[test_1025]") { + static constexpr const auto input{11}; + CHECK_FALSE(leetcode_1025::divisorGame(input)); + CHECK_FALSE(leetcode_1025::divisorGame2(input)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_DP_LEETCODE_1025_TEST_CPP diff --git a/algorithm/math/CMakeLists.txt b/algorithm/math/CMakeLists.txt index 063a61ff..85bb25bf 100644 --- a/algorithm/math/CMakeLists.txt +++ b/algorithm/math/CMakeLists.txt @@ -11,7 +11,7 @@ set(leetcode_order 9 29 43 50 60) LIST(APPEND leetcode_order 118_119 169 217 263) LIST(APPEND leetcode_order 268 283 338 343 372) LIST(APPEND leetcode_order 401 414 461 728 781) -LIST(APPEND leetcode_order 136 75 883) +LIST(APPEND leetcode_order 136 75 883 1018) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/math/leetcode_1018.cpp b/algorithm/math/leetcode_1018.cpp new file mode 100644 index 00000000..1904e02b --- /dev/null +++ b/algorithm/math/leetcode_1018.cpp @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +#include "leetcode_1018_test.hpp" + +namespace leetcode_1018 { + +vector leetcode_1018::prefixesDivBy5(const vector &nums) { + const auto nums_size{nums.size()}; + vector willreturn(nums_size); + for (size_t i{0}, num{0}; i < nums_size; ++i) { + num = (2 * num + nums[i]) % 5; + willreturn[i] = (num == 0); + } + return willreturn; +} + +} \ No newline at end of file diff --git a/algorithm/math/leetcode_1018_test.hpp b/algorithm/math/leetcode_1018_test.hpp new file mode 100644 index 00000000..95939bd4 --- /dev/null +++ b/algorithm/math/leetcode_1018_test.hpp @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +//@Tag Math +//@Tag 数学 +//@Tag 记忆化 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1018_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1018_TEST_HPP + +#include +#include +#include +#include + +namespace leetcode_1018 { +using std::vector; + +struct leetcode_1018 { + static vector prefixesDivBy5(const vector &nums); +}; + +using Catch::Matchers::Equals; + +TEST_CASE("1 [test_1018]", "[test_1018]") { + const vector input{0, 1, 1}; + const vector result{true, false, false}; + CHECK_THAT(result, Equals(leetcode_1018::prefixesDivBy5(input))); +} + +TEST_CASE("2 [test_1018]", "[test_1018]") { + const vector input{0, 1, 1}; + const vector result{true, false, false}; + CHECK_THAT(result, Equals(leetcode_1018::prefixesDivBy5(input))); +} + +TEST_CASE("3 [test_1018]", "[test_1018]") { + const vector input{0, 1, 1}; + const vector result{true, false, false}; + CHECK_THAT(result, Equals(leetcode_1018::prefixesDivBy5(input))); +} + +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1018_TEST_HPP + diff --git a/algorithm/stack/CMakeLists.txt b/algorithm/stack/CMakeLists.txt index 1a4ec335..60742d55 100644 --- a/algorithm/stack/CMakeLists.txt +++ b/algorithm/stack/CMakeLists.txt @@ -9,7 +9,7 @@ set(dependencies) set(leetcode_order 678 20 32 42 1614) LIST(APPEND leetcode_order 84_85 316 321 so_30) -LIST(APPEND leetcode_order 232) +LIST(APPEND leetcode_order 232 1021) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/stack/leetcode_1021.cpp b/algorithm/stack/leetcode_1021.cpp new file mode 100644 index 00000000..a38f54da --- /dev/null +++ b/algorithm/stack/leetcode_1021.cpp @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +//@Tag stack +//@Tag 栈 +//@Tag 输入保证合法 +#include "leetcode_1021_test.hpp" + +namespace leetcode_1021 { + +string leetcode_1021::removeOuterParentheses(const string &str) { + std::string will_return{}; + const auto str_size{str.size()}; + for (size_t i{0}; i < str_size; ++i) { + for (size_t depth{1}; depth != 0;) { + ++i; + if (str[i] == '(') { + ++depth; + } else { + --depth; + } + if (depth != 0) [[likely]] { + will_return += str[i]; + } + } + } + return will_return; +} + +} \ No newline at end of file diff --git a/algorithm/stack/leetcode_1021_test.hpp b/algorithm/stack/leetcode_1021_test.hpp new file mode 100644 index 00000000..a4158d5a --- /dev/null +++ b/algorithm/stack/leetcode_1021_test.hpp @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2020-2022 nanoseeds + +*/ +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_STACK_LEETCODE_1021_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_STACK_LEETCODE_1021_TEST_HPP + +#include +#include + +namespace leetcode_1021 { +using std::string; + +struct leetcode_1021 { + static string removeOuterParentheses(const string &str); +}; + +TEST_CASE("1 [test_1021]", "[test_1021]") { + static constexpr const char *const input{"(()())(())"}; + static constexpr const char *const result{"()()()"}; + CHECK(result == leetcode_1021::removeOuterParentheses(input)); +} + +TEST_CASE("2 [test_1021]", "[test_1021]") { + static constexpr const char *const input{"(())(()()())((()))"}; + static constexpr const char *const result{"()()()()(())"}; + CHECK(result == leetcode_1021::removeOuterParentheses(input)); +} + +TEST_CASE("3 [test_1021]", "[test_1021]") { + static constexpr const char *const input{"()()"}; + static constexpr const char *const result{""}; + CHECK(result == leetcode_1021::removeOuterParentheses(input)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_STACK_LEETCODE_1021_TEST_HPP diff --git a/algorithm/tree/CMakeLists.txt b/algorithm/tree/CMakeLists.txt index d28b8ce5..847ba1cf 100644 --- a/algorithm/tree/CMakeLists.txt +++ b/algorithm/tree/CMakeLists.txt @@ -14,7 +14,8 @@ LIST(APPEND leetcode_order 701 144 145 102 101) LIST(APPEND leetcode_order 112 235 653 so_32 so_26) LIST(APPEND leetcode_order 113 426 so_54 114 230) LIST(APPEND leetcode_order 236 543 199 814 872) -LIST(APPEND leetcode_order 894 897 938 965) +LIST(APPEND leetcode_order 894 897 938 965 993) +LIST(APPEND leetcode_order 1008 1022) LIST(TRANSFORM leetcode_order PREPEND leetcode_) set(dependencies ${dependencies} ${leetcode_order}) diff --git a/algorithm/tree/leetcode_1008.cpp b/algorithm/tree/leetcode_1008.cpp new file mode 100644 index 00000000..20ae1e7e --- /dev/null +++ b/algorithm/tree/leetcode_1008.cpp @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_1008_test.hpp" + +namespace leetcode_1008 { + + +// find fst > value in vec[left,right] +int32_t binary_search(const vector &vec, size_t left, size_t right, int32_t value) { + if (left > right) { + return 0; + } else if (left == right) { + return right; + } else if (left + 1 == right) { + return right; + }// 一定存在,所以上面这俩能直接判定 + while (left <= right) { + if (const size_t middle = (right - left) / 2 + left; + vec[middle] > value && vec[middle - 1] < value) { + return middle; + } else if (vec[middle] > value) { + right = middle; + } else { + left = middle + 1; + } + } + return 0; +} + +TreeNode *rec(const vector &preorder, int32_t left, int32_t right) { + if (left > right) { + return nullptr; + } else if (left == right) { + return new TreeNode{preorder[right]}; + } + TreeNode *const root = new TreeNode{preorder[left]}; + const int32_t leftPlus = ++left; + if (preorder[leftPlus] > root->val) { + root->right = rec(preorder, leftPlus, right); + return root; + } else if (preorder[right] < root->val) { + root->left = rec(preorder, leftPlus, right); + return root; + } + // there vec[leftPlus] < root->val < vec[right] + const auto bigger = binary_search(preorder, leftPlus, right, root->val); + if (bigger == 0) { + return root; + } + root->left = rec(preorder, leftPlus, bigger - 1); + root->right = rec(preorder, bigger, right); + return root; +} + +TreeNode *leetcode_1008::bstFromPreorder(const vector &preorder) { + const auto sizes{preorder.size() - 1}; + return rec(preorder, 0, sizes); +} + +} \ No newline at end of file diff --git a/algorithm/tree/leetcode_1008_test.hpp b/algorithm/tree/leetcode_1008_test.hpp new file mode 100644 index 00000000..fe40faf6 --- /dev/null +++ b/algorithm/tree/leetcode_1008_test.hpp @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag tree +//@Tag 树 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1008_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1008_TEST_HPP + +#include +#include +#include +#include +#include + +namespace leetcode_1008 { +using TreeNode = TREE_NODE::TreeNode; + +struct leetcode_1008 final { + static TreeNode *bstFromPreorder(const vector &preorder); +}; + +using TreeNodeLink = TREE_NODE::TreeNodeLink; + +TEST_CASE("test_case 4 [test_1008]", "[test_1008]") { + const vector input{1, 2, 3}; + const vector result{ + 1, + TreeNode::No, 2, + TreeNode::No, 3, + TreeNode::No, TreeNode::No + }; + TreeNode *const pointer = leetcode_1008::bstFromPreorder(input); + const TreeNodeLink treeNodeLink{pointer}; + CHECK(TREE_NODE::judge_equal(pointer, result)); +} + +TEST_CASE("test_case 1 [test_1008]", "[test_1008]") { + const vector input{8, 5, 1, 7, 10, 12}; + const vector result{ + 8, + 5, 10, + 1, 7, TreeNode::No, 12, + TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No + }; + TreeNode *const pointer = leetcode_1008::bstFromPreorder(input); + const TreeNodeLink treeNodeLink{pointer}; + CHECK(TREE_NODE::judge_equal(pointer, result)); +} + +TEST_CASE("test_case 2 [test_1008]", "[test_1008]") { + const vector input{1}; + const vector result{ + 1, + TreeNode::No, TreeNode::No + }; + TreeNode *const pointer = leetcode_1008::bstFromPreorder(input); + const TreeNodeLink treeNodeLink{pointer}; + CHECK(TREE_NODE::judge_equal(pointer, result)); +} + +TEST_CASE("test_case 3 [test_1008]", "[test_1008]") { + const vector input{4, 2}; + const vector result{ + 4, + 2, TreeNode::No, + TreeNode::No, TreeNode::No + }; + TreeNode *const pointer = leetcode_1008::bstFromPreorder(input); + const TreeNodeLink treeNodeLink{pointer}; + CHECK(TREE_NODE::judge_equal(pointer, result)); +} + +TEST_CASE("test_case 5 [test_1008]", "[test_1008]") { + const vector input{15, 13, 12, 18}; + const vector result{ + 15, + 13, 18, + 12, TreeNode::No, TreeNode::No, TreeNode::No, + TreeNode::No, TreeNode::No + }; + TreeNode *const pointer = leetcode_1008::bstFromPreorder(input); + const TreeNodeLink treeNodeLink{pointer}; + CHECK(TREE_NODE::judge_equal(pointer, result)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1008_TEST_HPP diff --git a/algorithm/tree/leetcode_1022.cpp b/algorithm/tree/leetcode_1022.cpp new file mode 100644 index 00000000..9decdbb4 --- /dev/null +++ b/algorithm/tree/leetcode_1022.cpp @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_1022_test.hpp" +#include + +namespace leetcode_1022 { +using std::stack; + +int32_t leetcode_1022::sumRootToLeaf(TreeNode *root) { + int32_t willreturn{0}; + for (stack tree{{root}}; !tree.empty();) { + TreeNode *const begin = tree.top(); + tree.pop(); + if (begin->left == nullptr && begin->right == nullptr) { + willreturn += begin->val; + continue; + } + if (begin->left != nullptr) { + begin->left->val += begin->val << 1; + tree.push(begin->left); + } + if (begin->right != nullptr) { + begin->right->val += begin->val << 1; + tree.push(begin->right); + } + } + return willreturn; +} + +} \ No newline at end of file diff --git a/algorithm/tree/leetcode_1022_test.hpp b/algorithm/tree/leetcode_1022_test.hpp new file mode 100644 index 00000000..22491347 --- /dev/null +++ b/algorithm/tree/leetcode_1022_test.hpp @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag tree +//@Tag 树 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1022_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1022_TEST_HPP + +#include +#include +#include +#include +#include + +namespace leetcode_1022 { +using TreeNode = TREE_NODE::TreeNode; + +struct leetcode_1022 final { + static int32_t sumRootToLeaf(TreeNode *root); +}; + + +using TreeNodeLink = TREE_NODE::TreeNodeLink; + +TEST_CASE("test_case 2 [test_1022]", "[test_1022]") { + const TreeNodeLink input{ + 1, + 0, 1, + 0, 1, 0, 1, + TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, + TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No + }; + static constexpr const auto result{22}; + CHECK(result == leetcode_1022::sumRootToLeaf(input[0])); +} + +TEST_CASE("test_case 1 [test_1022]", "[test_1022]") { + const TreeNodeLink input{ + 0, + TreeNode::No, TreeNode::No + }; + static constexpr const auto result{0}; + CHECK(result == leetcode_1022::sumRootToLeaf(input[0])); +} + +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1022_TEST_HPP diff --git a/algorithm/tree/leetcode_993.cpp b/algorithm/tree/leetcode_993.cpp new file mode 100644 index 00000000..0f93a069 --- /dev/null +++ b/algorithm/tree/leetcode_993.cpp @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +#include "leetcode_993_test.hpp" +#include + +namespace leetcode_993 { +using std::queue; + +bool leetcode_993::isCousins(TreeNode *root, int x, int y) { + for (queue now{{root}}, next{}; !now.empty(); std::swap(now, next)) { + for (bool getX{false}, getY{false}; !now.empty();) { + const auto head = now.front(); + now.pop(); + if (head->left != nullptr && head->right != nullptr) { + if ((head->left->val == x && head->right->val == y) || + (head->left->val == y && head->right->val == x)) { + return false; + } + } + const auto func = [&next, x, y, &getX, &getY](const TreeNode*const pointer) { + if (pointer != nullptr) { + next.push(pointer); + if (pointer->val == x) { + getX = true; + } else if (pointer->val == y) { + getY = true; + } + } + }; + func(head->left); + func(head->right); + if (getX && getY) { + return true; + } + } + } + return false; +} +} \ No newline at end of file diff --git a/algorithm/tree/leetcode_993_test.hpp b/algorithm/tree/leetcode_993_test.hpp new file mode 100644 index 00000000..91c4e512 --- /dev/null +++ b/algorithm/tree/leetcode_993_test.hpp @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: MIT +/* +CS203_DSAA_template + +Copyright (C) 2022 nanoseeds + +*/ +//@Tag tree +//@Tag 树 +#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_993_TEST_HPP +#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_993_TEST_HPP + +#include +#include +#include +#include +#include + +namespace leetcode_993 { +using TreeNode = TREE_NODE::TreeNode; + +struct leetcode_993 final { + static bool isCousins(TreeNode *root, int x, int y); +}; + +using TreeNodeLink = TREE_NODE::TreeNodeLink; + +TEST_CASE("test_case 1 [test_993]", "[test_993]") { + const TreeNodeLink input{ + 1, + 2, 3, + 4, TreeNode::No, TreeNode::No, TreeNode::No + }; + static constexpr const auto x{4}, y{3}; + CHECK_FALSE(leetcode_993::isCousins(input[0], x, y)); +} + +TEST_CASE("test_case 3 [test_993]", "[test_993]") { + const TreeNodeLink input{ + 1, + 2, 3, + TreeNode::No, 4, TreeNode::No, 5 + }; + static constexpr const auto x{5}, y{4}; + CHECK(leetcode_993::isCousins(input[0], x, y)); +} + +TEST_CASE("test_case 2 [test_993]", "[test_993]") { + const TreeNodeLink input{ + 1, + 2, 3, + TreeNode::No, 4, TreeNode::No + }; + static constexpr const auto x{5}, y{4}; + CHECK_FALSE(leetcode_993::isCousins(input[0], x, y)); +} +} +#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_993_TEST_HPP diff --git a/include/include/CS203_sequence.hpp b/include/include/CS203_sequence.hpp index a38a6aef..17041ab2 100644 --- a/include/include/CS203_sequence.hpp +++ b/include/include/CS203_sequence.hpp @@ -38,8 +38,7 @@ class CS203_sequence final : private nonCopyMoveAble { // default path1 is input and path2 is output explicit CS203_sequence(int32_t begin, int32_t end, int32_t max_length = -1) : begin(begin), end(end), - max_length(max_length) { - } + max_length(max_length) {} void set_prefix_of_filename(const string &prefixOfFileName) { prefix_of_file_name = prefixOfFileName; @@ -97,9 +96,9 @@ class CS203_sequence final : private nonCopyMoveAble { const vector sequence = same_length ? get_same_length_sequence() : get_sequence(); files_type will_return; for (const auto &item: sequence) { - string datain = prefix_of_file_name + item + "." + postfix_of_datain; - string dataout = prefix_of_file_name + item + "." + postfix_of_dataout; - string testout = prefix_of_file_name + item + "." + postfix_of_testout; + const string datain = prefix_of_file_name + item + "." + postfix_of_datain; + const string dataout = prefix_of_file_name + item + "." + postfix_of_dataout; + const string testout = prefix_of_file_name + item + "." + postfix_of_testout; will_return.push_back(std::make_tuple(datain, dataout, testout)); } return will_return;