-
Notifications
You must be signed in to change notification settings - Fork 643
/
8.4.cpp
49 lines (45 loc) · 1.09 KB
/
8.4.cpp
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
#include <iostream>
#include <vector>
using namespace std;
typedef vector<string> vs;
vs permu(string s){
vs result;
if(s == ""){
result.push_back("");
return result;
}
string c = s.substr(0, 1);
vs res = permu(s.substr(1));
for(int i=0; i<res.size(); ++i){
string t = res[i];
for(int j=0; j<=t.length(); ++j){
string u = t;
u.insert(j, c);
result.push_back(u);
}
}
return result; //调用result的拷贝构造函数,返回它的一份copy,然后这个局部变量销毁(与基本类型一样)
}
vs permu1(string s){
vs result;
if(s == ""){
result.push_back("");
return result;
}
for(int i=0; i<s.length(); ++i){
string c = s.substr(i, 1);
string t = s;
vs res = permu1(t.erase(i, 1));
for(int j=0; j<res.size(); ++j){
result.push_back(c + res[j]);
}
}
return result;
}
int main(){
string s = "abc";
vs res = permu1(s);
for(int i=0; i<res.size(); ++i)
cout<<res[i]<<endl;
return 0;
}