From 96254947699986c59f0dc63d69fd4b76bd3ed43e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 6 Jul 2026 19:08:08 +0200 Subject: Initial commit --- 01_introductory_problems/string_reorder_1743.cpp | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 01_introductory_problems/string_reorder_1743.cpp (limited to '01_introductory_problems/string_reorder_1743.cpp') diff --git a/01_introductory_problems/string_reorder_1743.cpp b/01_introductory_problems/string_reorder_1743.cpp new file mode 100644 index 0000000..d22041d --- /dev/null +++ b/01_introductory_problems/string_reorder_1743.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +int main() { + std::array a{}; + std::string s; + std::cin >> s; + for (auto c : s) a[c-'A']++; + + size_t i{0}, j{1}, tot{s.size()}, c{99}; + std::stringstream ss{}; + while (*std::max_element(a.begin(), a.end()) <= tot / 2) { + while (a[i] == 0) i++; + while (a[j] == 0 || j <= i) j++; + c = i == c ? j : i; + ss << (char)('A' + c); + a[c]--; + tot--; + } + + j = std::distance(a.begin(), std::max_element(a.begin(), a.end())); + while (a[j] > 1) { + if (tot == a[j]) { + std::cout << "-1\n"; + return 0; + } + while (a[i] == 0 || i == j) i++; + ss << (char)('A' + j) << (char)('A' + i); + a[i]--; + a[j]--; + tot -= 2; + } + std::cout << ss.str() << (char)('A' + j) << "\n"; +} -- cgit v1.3