7 more
This commit is contained in:
31
CSES - CSES Problem Set/Palindrome_Reorder.cpp
Normal file
31
CSES - CSES Problem Set/Palindrome_Reorder.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Palindrome Reorder
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
map<char, int> char_to_count;
|
||||
string s; cin >> s;
|
||||
for (auto c: s) char_to_count[c]++;
|
||||
string forward;
|
||||
char middle = ' ';
|
||||
for (auto &[ch, c]: char_to_count) {
|
||||
if (c & 1) {
|
||||
if (middle != ' ') {
|
||||
cout << "NO SOLUTION" << endl;
|
||||
return 0;
|
||||
}
|
||||
middle = ch;
|
||||
forward.append(string((c - 1) / 2, ch));
|
||||
} else {
|
||||
forward.append(string(c/2, ch));
|
||||
}
|
||||
}
|
||||
|
||||
cout << forward;
|
||||
if (middle != ' ') cout << middle;
|
||||
std::reverse(forward.begin(), forward.end());
|
||||
cout << forward << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user