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


在STL下瞬间变水题……

输入数组时用scanf()而不用gets()。原因是scanf()遇到空格时认为输入结束,而gets()会输入空格:样例显然是以空格而非 结束标志来判断的。所以,乖乖scanf()吧。

还有就是注意格式,这题又PE了,郁闷……看了看是IDE坏掉了。

放上代码:

#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

题目描述:
大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1六个排列。

任务描述:
给出某个排列,求出这个排列的下k个排列,如果遇到最后一个排列,则下1排列为第1个排列,即排列1 2 3…n。
比如:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。
Input

第一行是一个正整数m,表示测试数据的个数,下面是m组测试数据,每组测试数据第一行是2个正整数n( 1 <= n < 1024 )和k(1<=k<=64),第二行有n个正整数,是1,2 … n的一个排列。
Output

对于每组输入数据,输出一行,n个数,中间用空格隔开,表示输入排列的下k个排列。
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

一道全排列问题。用next_permutation轻松AC。注意头文件是algorithm即可。


#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类问题,查找排序中的下一序列。

简单AC代码:

#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;
}

 

嘘,老大哥在盯着你…………