cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

palindrome_reorder.cpp (978B)


      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 using ui = unsigned int;
      6 using l = long;
      7 using ul = unsigned long;
      8 using ll = long long;
      9 using ull = unsigned long long;
     10 
     11 using vi = vector<int>;
     12 using vui = vector<ui>;
     13 using vl = vector<l>;
     14 using vul = vector<ul>;
     15 using vll = vector<ll>;
     16 using vull = vector<ull>;
     17 
     18 void solve()
     19 {
     20 	string l;
     21 	getline(cin, l);
     22 
     23 	int a[26] = { 0 };
     24 
     25 	for (int i = 0; i < l.length(); i++)
     26 		a[l[i]-'A']++;
     27 
     28 	int odds = 0;
     29 	int odd_ix = -1;
     30 	for (int i = 0; i < 26; i++) {
     31 		if (a[i]%2)
     32 			odd_ix = i;
     33 		odds += a[i]%2;
     34 	}
     35 
     36 	if (odds > 1) {
     37 		cout << "NO SOLUTION\n";
     38 		return;
     39 	}
     40 
     41 	for (int i = 0; i < 26; i++) {
     42 		for(int j = 0; j < a[i]/2; j++) {
     43 			char x = i + 'A';
     44 			cout << x;
     45 		}
     46 	}
     47 
     48 	if (odd_ix != -1) {
     49 		char x = odd_ix + 'A';
     50 		cout << x;
     51 	}
     52 
     53 	for (int i = 25; i >= 0; i--) {
     54 		for(int j = 0; j < a[i]/2; j++) {
     55 			char x = i + 'A';
     56 			cout << x;
     57 		}
     58 	}
     59 	cout << "\n";
     60 }
     61 
     62 int main()
     63 {
     64 	ios::sync_with_stdio(0);
     65 	cin.tie(0);
     66 	solve();
     67 }