CSES – Palindrome Reorder

字串

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
string s;
int cnt['Z'+1];
//
signed main(){
    fastio;
    cin>>s;
    int n = s.size(), odd = 0;
    sort(s.begin(),s.end());
    for(int i=0;i<n;i++) cnt[s[i]]++;
    for(int i='A';i<='Z';i++) odd += cnt[i]%2;
    if(odd > 1){
        cout<<"NO SOLUTION\n";
        return 0;
    }
    string a,b;
    char c = '#';
    for(int i='A';i<='Z';i++){
        if(cnt[i]%2) c = i;
        for(int j=0;j<cnt[i]/2;j++){
            a += i;
            b += i;
        }
    }
    reverse(b.begin(),b.end());
    if(c == '#') cout<<a<<b<<"\n";
    else cout<<a<<c<<b<<"\n";
    return 0;
}

相關文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *