Advertisement
Saleh127

UVA 1209

Aug 4th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int minlength(string a,int lenn)
  4. {
  5. int ans=10000000,i;
  6. for(i=0;i<lenn-1;i++)
  7. {
  8. ans=min(ans,abs(a[i]-a[i+1]));
  9. }
  10. return ans;
  11. }
  12.  
  13. int main()
  14. {
  15. ios_base::sync_with_stdio(0);
  16. cin.tie(0);cout.tie(0);
  17.  
  18. string a,c,xx;
  19. while(getline(cin,a))
  20. {
  21. c=a;
  22. int i,j,k,ans=-10000000,length;
  23. int l=a.size();
  24. length=minlength(a,l);
  25. if(length>ans)
  26. {
  27. ans=length;
  28. xx=a;
  29. }
  30. for(i=0;i<10;i++)
  31. {
  32. if(prev_permutation(a.begin(),a.end()))
  33. {
  34. length=minlength(a,l);
  35. if(length>ans)
  36. {
  37. ans=length;
  38. xx=a;
  39. }
  40. if(length==ans)
  41. {
  42. xx=min(a,xx);
  43. }
  44. }
  45. }
  46. for(i=0;i<10;i++)
  47. {
  48. if(next_permutation(c.begin(),c.end()))
  49. {
  50. length=minlength(c,l);
  51. if(length>ans)
  52. {
  53. ans=length;
  54. xx=c;
  55. }
  56. if(length==ans)
  57. {
  58. xx=min(c,xx);
  59. }
  60. }
  61. }
  62. cout<<xx<<ans<<endl;
  63. }
  64. return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement