题解 | #全排列#
全排列
https://www.nowcoder.com/practice/5632c23d0d654aecbc9315d1720421c1
#include <iostream>
#include <map>
#include <queue>
using namespace std;
void getRankList(string s) {
map<string, bool> strMap;
queue<string> myQueue;
myQueue.push(s);
strMap[s] = true;
while (!myQueue.empty()) {
string current = myQueue.front();
myQueue.pop();
for (int i = 0; i <= (int)current.size() - 2;
i++) {//size()返回值是无符号数,做完减法也会当做无符号数
string temp = current;
swap(temp[i], temp[i + 1]);
if (strMap.find(temp) == strMap.end()) {
strMap[temp] = true;
myQueue.push(temp);
}
}
}
for (auto it = strMap.begin(); it != strMap.end(); it++) {
cout << it->first << endl;
}
}
int main() {
string a;
while (cin >> a ) { // 注意 while 处理多个 case
getRankList(a);
}
}
// 64 位输出请用 printf("%lld")
