-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMake_All_Permutation.cpp
More file actions
175 lines (126 loc) · 2.46 KB
/
Copy pathMake_All_Permutation.cpp
File metadata and controls
175 lines (126 loc) · 2.46 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void return_permutation(string str)
{
sort(str.begin(), str.end());
do
{
cout << str << endl;
} while (next_permutation(str.begin(), str.end()));
}
bool isMobile(int, string, const vector<int>&);
bool CheckMobiles(string str, const vector<int>& directions)
{
for (int i = 0; i < str.size(); i++)
{
if (isMobile(i, str, directions))
return true;
}
return false;
}
bool isMobile(int p, string str, const vector<int>& directions)
{
if (p == 0 && directions[p] == 0)
return false;
else if (p == str.size() - 1 && directions[p] == 1)
return false;
else
{
if (directions[p] == 0) //left move가 가능
{
if (str[p] > str[p - 1])
return true;
}
else
{
if (str[p] > str[p + 1])
{
return true;
}
}
}
return false; // 그외 모두 false
}
int FindLargest(string str, const vector<int>& directions)
{
vector<int> MobileNum;
for (int i = 0; i < str.size(); i++)
{
if (isMobile(i, str, directions))
MobileNum.push_back(i); //움직일수 있는 모든 수에 대한 인덱스 찾음
}
int LasrgestNum = MobileNum[0];
for (int p = 1; p < MobileNum.size(); p++)
{
if (str[MobileNum[p]] > LasrgestNum)
LasrgestNum = MobileNum[p];
}
return LasrgestNum;
}
void print_without_recursive(string str)
{
int k;
vector<int> directions;
sort(str.begin(), str.end());
for (const auto& i : str)
{
directions.push_back(0);
}
cout << str << endl;
while (CheckMobiles(str, directions))
{
k = FindLargest(str, directions);
if (directions[k] == 0)
{
swap(str[k], str[k - 1]);
swap(directions[k], directions[k - 1]);
k = k - 1;
}
else
{
swap(str[k], str[k+1]);
swap(directions[k], directions[k+1]);
k = k + 1;
}
for (int i = 0; i < str.size(); i++)
{
if (str[i] > str[k])
{
if (directions[i] == 0)
{
directions[i] = 1;
}
else
{
directions[i] = 0;
}
}
}
}
cout << str << endl;
}
void next_permutation(std::string str, std::string perm)
{
if (str.empty()) std::cout << perm << std::endl;
else
{
for (size_t i = 0; i < str.size(); ++i)
{
next_permutation(str.substr(1), perm + str[0]);
std::rotate(std::begin(str), std::begin(str) + 1, std::end(str));
}
}
}
void print_permutations_recursive(std::string str)
{
next_permutation(str, "");
}
int main()
{
return_permutation("apple");
cout << endl;
print_permutations_recursive("apple");
}