-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome Reorder.cpp
54 lines (54 loc) · 1.47 KB
/
Palindrome Reorder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while(t--){
string s;
cin>>s;
int frequency_arr[26]={};
for(auto i:s){
frequency_arr[(int)i - (int)'A']++;
}
bool oddFlag=false;
bool isPossible=true;
string front;
string backRev;
int last_add=-1;
for(int i=0;i<26;i++){
if(frequency_arr[i]&1){
if(oddFlag){
isPossible=false;
break;
}
else{
oddFlag=true;
last_add = i;
}
}
else{
for(int j=0;j<frequency_arr[i]/2;j++){
front.push_back((char)('A'+i));
backRev.push_back((char)('A'+i));
}
}
}
if(isPossible){
string answer;
if(last_add!=-1){
for(int j=0;j<frequency_arr[last_add];j++){
front.push_back((char)('A'+last_add));
}
}
reverse(backRev.begin(), backRev.end());
answer = front+ backRev;
cout<<answer<<"\n";
}
else{
cout<<"NO SOLUTION"<<"\n";
}
}
return 0;
}