1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <map> using namespace std; char chain[52] = {'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','Z','z'}; bool cmp(char a,char b){ if (a == b) { return false; } bool flag = false; for (int i = 0;i < 52;i++) { if (a == chain[i]) { flag = true; break; } if (b == chain[i]) { flag = false; break; } } return flag; } int main(){ int cnt; cin >> cnt; while (cnt--) { string toProc; cin >> toProc; while (next_permutation(toProc.begin(), toProc.end(),cmp)) { } cout << toProc << endl; while (next_permutation(toProc.begin(), toProc.end(),cmp)) { cout << toProc << endl; } } return 0; } |
next_permutation的返回bool值的含义是接下来还有没有可以用的排列。
然后要注意这个题的排序方式是特殊的,要自己构建排序函数。
排序函数返回值的意思是:如果返回true,则交换两个值,返回false,则不对两个值操作,所以如果a==b一定要返回false,否则会死循环。