——by A Code Rabbit
Description
输入一个序列,要求输出这个序列所属的所有全排列。
Types
Brute Force :: Elementary Skills
Analysis
用 perv_permutation( )把序列变成所有全排列最小的那个,然后再用next_permutation( )边变大边输出。
要注意两个函数会发生“回绕”。
Solution
// UVaOJ 10098
// Generating Fast
// by A Code Rabbit
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int LIMITS = 12;
int n;
char str[LIMITS];
int main() {
scanf("%d", &n);
getchar();
while (n--) {
gets(str);
int len = strlen(str);
while (prev_permutation(str, str + len)) {
}
next_permutation(str, str + len);
do {
puts(str);
} while (next_permutation(str, str + len));
printf("\n");
}
return 0;
}
参考资料:无
本文介绍了一种使用prev_permutation和next_permutation函数生成输入序列的所有全排列的方法,并提供了完整的C++实现代码。该方法首先将序列调整到最小的全排列状态,然后依次生成并输出所有的全排列。
383

被折叠的 条评论
为什么被折叠?



