Skip to content

Instantly share code, notes, and snippets.

@yjfvictor
Created October 4, 2014 07:09
Show Gist options
  • Save yjfvictor/3d62493a0318cbbcce0d to your computer and use it in GitHub Desktop.
Save yjfvictor/3d62493a0318cbbcce0d to your computer and use it in GitHub Desktop.
#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