-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations.cpp
64 lines (52 loc) · 1.75 KB
/
Permutations.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
// Lets make some permutate fun.
// Warning: Recursion ahead.
void permutate (vector<string> &strArray, string strBuffer, int intStart, int intEnd) {
int intIndex = 0;
if (intStart == intEnd) {
strArray.push_back(strBuffer);
}
else {
// Time to split the permutations.
for (intIndex = intStart; intIndex <= intEnd; intIndex++) {
// Swapping letters.
swap (strBuffer[intStart], strBuffer[intIndex]);
// Recursion called
permutate (strArray, strBuffer, intStart + 1, intEnd);
// Swap letters back.
swap (strBuffer[intStart], strBuffer[intIndex]);
}
}
}
int main(void) {
string strBuffer;
vector<string> strArray;
bool bMatchFound = false;
int intIndexA = 0, intIndexB = 0;
cout << "Warning: This is exponential recursive processing.." << endl;
cout << "Meaning more letters, longer times!" << endl;
cout << "Enter a string: ";
getline(cin, strBuffer);
cout << endl;
int intBufferSize = strBuffer.size();
permutate (strArray, strBuffer, 0, intBufferSize - 1);
// Cleaning up the multiples.
// Any string with multiple letters matching will duplicated the result permutations.
for (intIndexA = 0; intIndexA < (int) strArray.size(); intIndexA++) {
for (intIndexB = intIndexA + 1; intIndexB < (int) strArray.size(); intIndexB++) {
if (strArray[intIndexA].compare(strArray[intIndexB]) == 0) { bMatchFound = true; break; }
}
if (bMatchFound) { strArray.erase(strArray.begin() + intIndexA); bMatchFound = false; }
}
cout << "Number of Permutates: " << strArray.size() << endl;
/*
for (string strElement : strArray) {
cout << strElement << endl;
}
*/
return 0;
}