## POJ3785：The Next Permutation——全排列问题

Description

For this problem, you will write a program that takes a (possibly long) string of decimal digits, and outputs the permutation of those decimal digits that has the next larger value (as a decimal number) than the input number. For example:

123 -> 132
279134399742 -> 279134423799

It is possible that no permutation of the input digits has a larger value. For example, 987.
Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by up to 80 decimal digits which is the input value.
Output

For each data set there is one line of output. If there is no larger permutation of the input digits, the output should be the data set number followed by a single space, followed by the string BIGGEST. If there is a solution, the output should be the data set number, a single space and the next larger permutation of the input digits.
Sample Input

3
1 123
2 279134399742
3 987
Sample Output

1 132
2 279134423799
3 BIGGEST

```#include
#include
#include
char num[1010];
using namespace std;
int main()
{
int p, n, cache;
scanf("%d", &p);
while (p--)
{
memset(num, 0, sizeof(num));
cache = n = 0;
scanf("%d", &cache);
scanf("%s", num);
printf("%d ", cache);
if (next_permutation(num, num + strlen(num)))
puts(num);
else printf("BIGGESTn");
}
return 0;
}
```

## POJ1833：排列——全排列问题

Description

Input

Output

Sample Input

3
3 1
2 3 1
3 1
3 2 1
10 2
1 2 3 4 5 6 7 8 9 10
Sample Output

3 1 2
1 2 3
1 2 3 4 5 6 7 9 8 10

```#include
#include
using namespace std;
int cache[2000];
int main()
{
int N;
scanf("%d", &N);
while (N--)
{
int n, k, i;
scanf("%d%d",&n,&k);
for (i = 0; i < n; i++)
scanf("%d", &cache[i]);
for (i = 0; i < k; i++)
next_permutation(cache, cache + n);
printf("%d", cache[0]);
for (i = 1; i < n; i++)
printf(" %d", cache[i]);
printf("n");
}
return 0;
}
```

## POJ1146：ID Codes——全排列问题

Description

It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical measure–all citizens are to have a tiny microcomputer surgically implanted in their left wrists. This computer will contains all sorts of personal information as well as a transmitter which will allow people’s movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons.)

An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. The set of characters for any given code is chosen somewhat haphazardly. The complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. Thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set.

For example, suppose it is decided that a code will contain exactly 3 occurrences of `a’, 2 of `b’ and 1 of `c’, then three of the allowable 60 codes under these conditions are:
abaabc

abaacb

ababac

These three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order.

Write a program to assist in the issuing of these identification codes. Your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message `No Successor’ if the given code is the last in the sequence for that set of characters.
Input

Input will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single #.
Output

Output will consist of one line for each code read containing the successor code or the words ‘No Successor’.
Sample Input

abaacb
cbbaa
#
Sample Output

ababac
No Successor

C++中STL的next_permutation类问题，查找排序中的下一序列。

```#include
#include
using namespace std;
int main()
{
int lenth;
char s[51];
while (scanf("%s", s) != EOF)
{
if (s[0] == '#')
break;
lenth = strlen(s);
if (next_permutation(s, s + lenth))
printf("%sn", s);
else
printf("No Successorn");
}
return 0;
}
```