Advertisement
a53

palindrom3

a53
Oct 15th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string nr;
  4. int cf[11],cmax,od=-1,c,nrod;
  5.  
  6. bool canFormPalindrome(string str)
  7. {
  8. for(int i=0;str[i];++i)
  9. {
  10. ++cf[str[i]-'0'];
  11. if(str[i]-'0'>cmax)
  12. cmax=str[i]-'0';
  13. }
  14. int odd=0;
  15. for(int i=0;i<10;++i)
  16. {
  17. if (cf[i]&1)
  18. ++odd,od=i;
  19. if(odd > 1)
  20. return false;
  21. }
  22. return true;
  23. }
  24.  
  25. int main()
  26. {
  27. ifstream f("palindrom3.in");
  28. f>>nr;
  29. f.close();
  30. ofstream g("palindrom3.out");
  31. if(nr.size()==1)
  32. {
  33. g<<nr;
  34. return 0;
  35. }
  36. if(canFormPalindrome(nr))
  37. {
  38. for(int i=cmax;i>=0;--i)
  39. {
  40. if(cf[i])
  41. for(int j=1;j<=cf[i]/2;++j)
  42. g<<i;
  43. if(i!=od)
  44. cf[i]/=2;
  45. else
  46. if(od>=0)
  47. cf[i]=cf[i]/2+1;
  48. }
  49. if(od>=0&&cf[od])
  50. g<<od,--cf[od];
  51. for(int i=0;i<=cmax;++i)
  52. {
  53. if(cf[i])
  54. for(int j=1;j<=cf[i];++j)
  55. g<<i;
  56. }
  57. }
  58. else
  59. g<<0;
  60. g.close();
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement