fork download
  1. /*
  2.   ID: #
  3. Copyright: mintzy###
  4. Author: Nguyen Ngoc Minh
  5. Date: 7/22/2017 5:14:11 PM
  6. Description:
  7. */
  8.  
  9. #include <bits/stdc++.h>
  10.  
  11. using namespace std;
  12. const int MN = 100;
  13. char s[MN] ;
  14. void Swap(char &a,char &b) {
  15. char t;
  16. t = a;
  17. a = b;
  18. b = t;
  19. }
  20. bool nextPermutation(char * s) {
  21. int len = strlen(s);
  22. int k = len - 2;
  23. while(k >= 0 && s[k] >= s[k + 1] ) k--;
  24. if(k < 0) return false;
  25. int t = len - 1;
  26. while(s[t] < s[k] ) t--;
  27. Swap(s[t],s[k]);
  28. int l,r;
  29. l = k + 1;
  30. r = len - 1;
  31. while(l < r) {
  32. Swap(s[l] , s[r]);
  33. l++;
  34. r--;
  35. }
  36. return true;
  37. }
  38. int main(){
  39. // Your code goes here
  40. int test,stt;
  41. cin >> test;
  42. while(test--) {
  43. cin >> stt >> s;
  44. cout << stt << " ";
  45. if(nextPermutation(s)) cout << s << endl;
  46. else cout << "BIGGEST\n";
  47. }
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 2744KB
stdin
5
1 234
2 324
3 33333
4 98826542
5 12345
stdout
1 243
2 342
3 BIGGEST
4 98822456
5 12354