fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define rep(i, n) for (int i = 0; i < (n); i++)
  4.  
  5. bool compare(char a,char b){
  6. return a>=b;
  7. }
  8.  
  9. int main(){
  10. string a;cin>>a;
  11. int len=a.size();
  12. int fre[26]={0};
  13. rep(i,len){
  14. fre[a[i]-'a']++;
  15. }
  16. string ans="";
  17. rep(i,26){
  18. while(fre[i]>1 && i!=25){//For every two frequencies for given char append one char.
  19. char t='a'+i+1;
  20. //cout<<t<<" "<<ans<<endl;
  21. ans.push_back(t);
  22. fre[i]-=2;
  23. }
  24. while(i==25 && fre[i]>0){//Append all 'z' characters found in string
  25. char t='a'+i;
  26. ans.push_back(t);
  27. fre[i]--;
  28. }
  29. if(fre[i]==1){
  30. char t='a'+i;
  31. ans.push_back(t);
  32. //cout<<t<<" "<<ans<<endl;
  33. fre[i]--;
  34. }
  35. }
  36. sort(ans.begin(),ans.end(),compare);
  37. for(int i=0;ans[i]!='\0';i++){
  38. cout<<ans[i];
  39. }
  40. return 0;
  41. }
  42.  
  43.  
  44.  
  45.  
Success #stdin #stdout 0s 15240KB
stdin
zyayz
stdout
zzza