字符串全排列

来源:互联网 发布:如何查看网络稳定 编辑:程序博客网 时间:2024/05/08 07:24
#include <iostream>#include <vector>#include <set>#include <algorithm>using namespace std;set<string> res;void fun(string str, int pos) {    if (str.length() == pos) {        res.insert(str);        return ;    }    for (int i = pos; i < str.length(); i ++) {        swap(str[i], str[pos]);        fun(str, pos + 1);        swap(str[i], str[pos]);    }}vector<string> permutation(string str) {    res.clear();    vector<string> vs;    if (str.length() == 0)        return vs;    fun(str, 0);    set<string>::iterator it;    for (it = res.begin(); it != res.end(); it ++)        vs.push_back(*it);    return vs;}int main() {    string str = "abc";    vector<string> vs = permutation(str);    for (int i = 0; i < vs.size(); i ++)        cout << vs[i] << endl;}
0 0
原创粉丝点击