-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.cpp
More file actions
104 lines (83 loc) · 2.28 KB
/
permutations.cpp
File metadata and controls
104 lines (83 loc) · 2.28 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
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
void print_stack(stack<char> &st) {
stack<char> tmp;
while(!st.empty()) {
char c = st.top();
st.pop();
tmp.push(c);
}
while(!tmp.empty()) {
char c= tmp.top();
tmp.pop();
st.push(c);
cout<<c;
}
cout<<"\n";
}
/*
eg:
prefix = abc (c being top of stack)
suffix = defg (d being front of queue)
If size of prefix is "n" and size of suffix is "m", then there are "m" ways of populating the position "n+1"th position.
For each way of filling position "n+1" there are m-1 remaining characters which need to be permuted.
eg:
permute (abc, defg)
abc+d + permute(efg)
abc+e + permute(fgd)
abc+f + permute(gde)
abc+g + permute(def)
This cycling of elements to be considered for "n+1"th position can be achieved by maintaining suffix as a queue
iterating over the length of the queue
pop from front of queue
push to top of stack (i.e new prefix)
generate all permutations for the remaining items in suffix queue.
pop from top of stack
push to back of queue
Once the iteration is complete, the queue will be in same ordering of elements as before the iteration
to permute a whole string
permute(empty_stack, queue_with_all_chars_in_string)
*/
void permute (stack<char> &prefix, queue<char> &suffix, int &perm_count) {
if(suffix.size() == 0) {
//if it is required to print all combinations of lenght "n", then insted of suffix.size() == 0, check prefix.size() == n
print_stack(prefix);
perm_count++;
return;
}
for(int i = 0; i < suffix.size(); i++) {
char c = suffix.front();
suffix.pop();
prefix.push(c);
permute(prefix, suffix, perm_count);
c = prefix.top();
prefix.pop();
suffix.push(c);
}
}
int main() {
stack<char> prefix;
queue<char> suffix;
string input;
int perm_count = 0;
while(1) {
cout << "Enter the string: ";
cin >> input;
cout << "\n";
for(string::iterator it = input.begin(); it != input.end(); it++) {
if(*it != '\0')
suffix.push(*it);
}
permute(prefix, suffix, perm_count);
cout << "Number of permutations = "<<perm_count << "\n";
while(!prefix.empty())
prefix.pop();
while(!suffix.empty())
suffix.pop();
perm_count = 0;
}
}