diff --git a/2202/220207-CN.cpp b/2202/220207-CN.cpp new file mode 100644 index 0000000..49a30b7 --- /dev/null +++ b/2202/220207-CN.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +/** + * 1405. Longest Happy String + * A string s is called happy if it satisfies the following conditions: + * s only contains the letters 'a', 'b', and 'c'. + * s does not contain any of "aaa", "bbb", or "ccc" as a substring. + * s contains at most a occurrences of the letter 'a'. + * s contains at most b occurrences of the letter 'b'. + * s contains at most c occurrences of the letter 'c'. + * Given three integers a, b, and c, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string "". + * A substring is a contiguous sequence of characters within a string. + */ + +class Solution { + static std::string gen(char xc, int x, char yc, int y) { + if (x < y) { + std::swap(xc, yc); + std::swap(x, y); + } + if (!y) + return std::string(std::min(2, x), xc); + std::string ret; + if (x > (y << 1)) { + for (int i = 0; i < y; ++i) + ret += std::string{xc, xc, yc}; + ret += std::string(std::min(2, x - (y << 1)), xc); + } else { + for (int i = x - y; i--; ) + ret += std::string{xc, xc, yc}; + for (int i = y - (x - y); i--; ) + ret += std::string{xc, yc}; + } + return ret; + } +public: + static std::string longestDiverseString(int a, int b, int c) { + std::vector> s {{a, 'a'}, {b, 'b'}, {c, 'c'}}; + std::sort(s.begin(), s.end(), [](const std::pair& x, const std::pair& y) { return x.first > y.first; }); + + if (s[0].first > 2 * (s[1].first + s[2].first)) + return gen(s[0].second, s[1].first << 1, s[1].second, s[1].first) + gen(s[0].second, s[0].first - (s[1].first << 1), s[2].second, s[2].first); + + int for2 = std::min(s[0].first >> 1, s[2].first << 1), for1 = s[0].first - for2; + if (for1 < s[1].first && s[2].first < for2) + return gen(s[0].second, for2, s[2].second, s[2].first) + gen(s[0].second, for1, s[1].second, s[1].first); + return gen(s[0].second, for1, s[1].second, s[1].first) + gen(s[0].second, for2, s[2].second, s[2].first); + } +}; + +int main() { + std::cout << Solution::longestDiverseString(1, 0, 3); + return 0; +} diff --git a/2202/CMakeLists.txt b/2202/CMakeLists.txt index a87229c..7a81450 100644 --- a/2202/CMakeLists.txt +++ b/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220206.cpp) +ADD_EXECUTABLE(2202 220207-CN.cpp)