aboutsummaryrefslogtreecommitdiff
path: root/01_introductory_problems/palindrome_reorder_1755.cpp
diff options
context:
space:
mode:
Diffstat (limited to '01_introductory_problems/palindrome_reorder_1755.cpp')
-rw-r--r--01_introductory_problems/palindrome_reorder_1755.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/01_introductory_problems/palindrome_reorder_1755.cpp b/01_introductory_problems/palindrome_reorder_1755.cpp
new file mode 100644
index 0000000..8ebf4f4
--- /dev/null
+++ b/01_introductory_problems/palindrome_reorder_1755.cpp
@@ -0,0 +1,35 @@
1#include <iostream>
2#include <string>
3
4void printn(char c, int n) {
5 for (int i = 0; i < n; i++)
6 std::cout << c;
7}
8
9int main() {
10 std::string s;
11 int odd{0}, a['Z'+1] = {0};
12 char oddc{0};
13 std::cin >> s;
14 for (auto c : s)
15 a[(size_t)c]++;
16 for (char c = 'A'; c <= 'Z'; c++) {
17 if (a[(size_t)c] % 2) {
18 odd++;
19 oddc = c;
20 }
21 }
22 if (odd > 1) {
23 std::cout << "NO SOLUTION\n";
24 } else {
25 for (char c = 'A'; c <= 'Z'; c++)
26 if (c != oddc)
27 printn(c, a[(size_t)c]/2);
28 if (odd)
29 printn(oddc, a[(size_t)oddc]);
30 for (char c = 'Z'; c >= 'A'; c--)
31 if (c != oddc)
32 printn(c, a[(size_t)c]/2);
33 std::cout << std::endl;
34 }
35}

Generated with cgit - Back to sebastiano.tronto.net