Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string s,res;
  8. int n,k;
  9. char min_char;
  10. cin >> n >> k >> s;
  11. res = s;
  12. for (int j = 0; j <= 25; j++)
  13. if (s.find('a' + j) != -1)
  14. {
  15. min_char = 'a' + j;
  16. break;
  17. }
  18.  
  19.  
  20. if (k > n)
  21. {
  22. while (res.size() < k)
  23. res += min_char;
  24. cout << res << endl;
  25. return 0;
  26. }
  27.  
  28. while (res.size() > k)
  29. res.pop_back();
  30.  
  31. for (int i = res.size() - 1; i >= 0; i--)
  32. {
  33. bool f = true;
  34. for (int j = 0; j <= 25; j++)
  35. if (s.find('a' + j) != -1 && 'a' + j > s[i])
  36. {
  37. res[i] = 'a' + j;
  38. f = false;
  39. break;
  40. }
  41.  
  42. if (!f)
  43. break;
  44. else
  45. res[i] = min_char;
  46. }
  47. cout << res << endl;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement