问题 B: 全排列
时间限制: 1 Sec 内存限制: 32 MB
献花: 97 解决: 32
[献花][花圈][TK题库]
题目描述
给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列。
我们假设对于小写字母有’a’ < ‘b’ < … < ‘y’ < ‘z’,而且给定的字符串中的字母已经按照从小到大的顺序排列。
输入
输入只有一行,是一个由不同的小写字母组成的字符串,已知字符串的长度在1到6之间。
输出
输出这个字符串的所有排列方式,每行一个排列。要求字母序比较小的排列在前面。字母序如下定义:
已知S = s1s2…sk , T = t1t2…tk,则S < T 等价于,存在p (1 <= p <= k),使得
s1 = t1, s2 = t2, …, sp - 1 = tp - 1, sp < tp成立。
注意每组样例输出结束后接一个空行。
样例输入
xyz
样例输出
xyz
xzy
yxz
yzx
zxy
zyx
提示
用STL中的next_permutation会非常简洁。
#define _CRT_SECURE_NO_WARNINGS
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
using namespace std;
int main()
{
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG
string str;
while (cin >> str)
{
do
{
printf("%s\n", str.c_str());
} while (next_permutation(str.begin(), str.end()));
printf("\n");
}
return 0;
}
/**************************************************************
Problem: 1959
User: Sharwen
Language: C++
Result: 升仙
Time:1 ms
Memory:1708 kb
****************************************************************/