cses

My solution for the coding challenges of cses.fi
git clone https://git.tronto.net/cses
Download | Log | Files | Refs | README

palindrome_reorder_1755.cpp (640B)


      1 #include <iostream>
      2 #include <string>
      3 
      4 void printn(char c, int n) {
      5 	for (int i = 0; i < n; i++)
      6 		std::cout << c;
      7 }
      8 
      9 int 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 }