Created
October 4, 2014 07:09
-
-
Save yjfvictor/3d62493a0318cbbcce0d to your computer and use it in GitHub Desktop.
The Next Permutation http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=8154&pid=1004&ojid=1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstdlib> | |
#include <cstring> | |
#include <algorithm> | |
#include <functional> | |
using namespace std; | |
#define MAX_LENGTH 1000 | |
int main() | |
{ | |
int test_cases; | |
static char number[MAX_LENGTH]; | |
scanf("%d", &test_cases); | |
for (int i = 1; i <= test_cases; ++ i) | |
{ | |
scanf("%*d %s", number); | |
int len = strlen(number); | |
bool found_greater = false; | |
bool all_equal = true; | |
for (int j = len - 2; j >= 0; -- j) | |
{ | |
if (number[j] == number[j+1]) | |
continue; | |
all_equal = false; | |
if (number[j] < number[j+1]) | |
{ | |
found_greater = true; | |
char smallest = '9' + 1; | |
int smallest_subscript = 0; | |
for (int k = j + 1; k < len; ++ k) | |
{ | |
if (number[k] > number[j] && number[k] < smallest) | |
{ | |
smallest = number[k]; | |
smallest_subscript = k; | |
} | |
} | |
swap(number[j], number[smallest_subscript]); | |
sort(&number[j+1], &number[len]); | |
break; | |
} | |
} | |
if (all_equal || found_greater) | |
printf("%d %s\n", i, number); | |
else | |
printf("%d BIGGEST\n", i); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment