Beingamanforever

Palindrome Reorder, CSES

Feb 8th, 2025
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <atcoder/all>
  3. using namespace std;
  4. #define int long long
  5. #define all(x) (x).begin(), (x).end()
  6. typedef vector<int> vi;
  7. typedef vector<vi> vvi;
  8. typedef vector<pair<int, int>> vpi;
  9. typedef pair<int, int> pi;
  10. #define f first
  11. #define s second
  12. #define pb push_back
  13. #define endl "\n"
  14. #define yes cout << "YES" << endl
  15. #define no cout << "NO" << endl
  16. int dx[] = {-1, 0, 1, 0};
  17. int dy[] = {0, 1, 0, -1};
  18. const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
  19. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  20. // -----------------------------------------------------------------------------
  21.  
  22. void solve()
  23. {
  24.     string s;
  25.     cin >> s;
  26.     int n = s.size(), odd = 0;
  27.     vi cnt(26, 0);
  28.     for (char c : s)
  29.     {
  30.         cnt[c - 'A']++;
  31.     }
  32.     char c;
  33.     for (int i = 0; i < 26; i++)
  34.     {
  35.         if (cnt[i] & 1)
  36.         {
  37.             odd++;
  38.             c = i + 'A';
  39.         }
  40.     }
  41.     if ((n & 1) && odd != 1 || (!(n & 1) && odd > 0))
  42.     {
  43.         cout << "NO SOLUTION" << endl;
  44.         return;
  45.     }
  46.     // valid cases
  47.     string start = "", end = "", mid = "";
  48.     if (c)
  49.     {
  50.         mid = c;
  51.     }
  52.     for (int i = 0; i < 26; i++)
  53.     {
  54.         if (cnt[i] > 0)
  55.         {
  56.             string temp(cnt[i] / 2, i + 'A');
  57.             start = start + temp;
  58.             end = temp + end;
  59.         }
  60.     }
  61.     cout << (start + mid + end) << endl;
  62.     return;
  63. }
  64.  
  65. signed main()
  66. {
  67.     // __START__;
  68.     ios_base::sync_with_stdio(false);
  69.     cin.tie(NULL);
  70.     cout.tie(NULL);
  71.     int t = 1;
  72.     // cin >> t;
  73.     while (t--)
  74.     {
  75.         solve();
  76.     }
  77.     // __END__;
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment