Repository navigation
Expand file tree
/
Copy pathcombine_perm.cpp
More file actions
78 lines (69 loc) · 1.59 KB
/
Copy pathcombine_perm.cpp
File metadata and controls
78 lines (69 loc) · 1.59 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//字符串排列组合问题
//Wed Sep 10 07:24:46 PDT 2014
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
//根据字符串输出其组合
//设置容器vector,从第一个字符a开始:
// 1. vector加入a,递归调用i-1
// 2. vector不加a,递归调用i
void CombineCore(char* str, int num, vector<char> &vec)
{
if (*str == '\0')
return;
if (num == 0)
{
vector<char>::iterator ite = vec.begin();
for (; ite != vec.end(); ++ite)
cout << *ite << "\t";
cout << endl;
return;
}
vec.push_back(*str);
CombineCore(str+1, num-1, vec);
vec.pop_back();
CombineCore(str+1, num, vec);
}
void Combine(char* str)
{
int len = strlen(str);
vector<char> vec;
for (int i = 1; i < len; ++i)
CombineCore(str, i, vec);
}
//根据字符串,输出其排列
void PermutationCore(char *str, int index)
{
if (index == strlen(str))
{
cout << str << endl;
return;
}
for (int i = index; i < strlen(str); ++i)
{
swap(str[i], str[index]);
//char temp = str[i];
//str[i] = str[index];
//str[index] = temp;
PermutationCore(str, index + 1);
swap(str[i], str[index]);
//temp = str[i];
//str[i] = str[index];
//str[index] = temp;
}
}
void Permutation(char *str)
{
//int len = strlen(str);
//for (int i = 0; i < len; i++)
PermutationCore(str, 0);
}
int main()
{
char str[] = "abc";
//Combine(str);
Permutation(str);
return 0;
}