Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Design and analysis of algorithms / Practicum / Maycamp-198_Palindrome.cpp
diff options
context:
space:
mode:
1 files changed, 38 insertions, 0 deletions
-rw-r--r--Year 2/Design and analysis of algorithms/Practicum/Maycamp-198_Palindrome.cpp38
diff --git a/Year 2/Design and analysis of algorithms/Practicum/Maycamp-198_Palindrome.cpp b/Year 2/Design and analysis of algorithms/Practicum/Maycamp-198_Palindrome.cpp
new file mode 100644
index 0000000..2308171
--- /dev/null
+++ b/Year 2/Design and analysis of algorithms/Practicum/Maycamp-198_Palindrome.cpp
@@ -0,0 +1,38 @@
+#include <cstdio>
+#include <string>
+#include <deque>
+using namespace std;
+
+int letters[200] = { 0 };
+
+int main() {
+ char input[1000010] = { '\0' };
+ scanf("%s", input);
+ for (int i = 0; input[i] != '\0'; i++) {
+ letters[(int)input[i]]++;
+ }
+ deque<char> out;
+ int odd = -1;
+ for (int i = 0; i < 200; i++) {
+ if (letters[i] % 2 == 1) {
+ if (odd < 0) odd = i;
+ else {
+ printf("IMPOSSIBLE");
+ return 0;
+ }
+ }
+ }
+ if (odd > 0) {
+ out.push_back((char)odd);
+ letters[odd]--;
+ }
+ for (int i = 199; i >= 0; i--) {
+ for (int j = letters[i] / 2; j < letters[i]; j++) {
+ out.push_front((char)i);
+ out.push_back((char)i);
+ }
+ }
+ for (char& ch : out) {
+ printf("%c", ch);
+ }
+}