Created
February 2, 2025 14:48
-
-
Save OriLiMu/ffc0a052c36168d58ec5b86575567647 to your computer and use it in GitHub Desktop.
等长字符串所有组合
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
vector<string> generateAllPermutations(const vector<string> &strings) { | |
vector<string> result; | |
vector<string> temp = strings; | |
// 排序以便生成所有排列 | |
sort(temp.begin(), temp.end()); | |
do { | |
string combo; | |
for (const string &s : temp) { | |
combo += s; | |
} | |
result.push_back(combo); | |
} while (next_permutation(temp.begin(), temp.end())); | |
return result; | |
} | |
int main() { | |
vector<string> strings = {"ab", "cd", "ef"}; | |
vector<string> permutations = generateAllPermutations(strings); | |
cout << "所有排列组合:" << endl; | |
for (const string &perm : permutations) { | |
cout << perm << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment