Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_set>
- #include <stack>
- using namespace std;
- #include <string>
- #include <sstream>
- template <typename T>
- std::string to_string(T value)
- {
- std::ostringstream os;
- os << value;
- return os.str();
- }
- string rotateNFromLeft(string st, int n)
- {
- return st.substr(n-1,1) + st.substr(0,n-1) + st.substr(n);
- }
- string rotateNFromRight(string st, int n)
- {
- int size = st.length();
- return st.substr(0, size-n) + st.substr(size-1) + st.substr(size-n, n-1);
- }
- int main(int argc, char *argv[]) {
- cout << "Please enter the number of items to be permuted--> ";
- int i;
- cin >> i;
- cout << "Testing permutations of " + to_string(i) + " elements" << endl;
- for (int left = 1; left <= i; left++)
- {
- for (int right = 1; right <= left; right++)
- {
- if (left+right > i)
- {
- string nums = "";
- char ch = 'a';
- for (int x=1; x<=i; x++)
- {
- nums += ch;
- ch++;
- }
- // Create the "unordered set" into which the strings will be placed
- unordered_set<string> theset;
- theset.insert(nums);
- // Create the stack which will contain all the ones yet to be tested
- stack<string> thestack;
- thestack.push(nums);
- // Loop until there is nothing left in the stack to test
- while (thestack.size() > 0)
- {
- string nexttotest = thestack.top();
- thestack.pop();
- string leftrotate = rotateNFromLeft(nexttotest, left);
- if (theset.insert(leftrotate).second == true)
- thestack.push(leftrotate);
- string rightrotate = rotateNFromRight(nexttotest, right);
- if (theset.insert(rightrotate).second == true)
- thestack.push(rightrotate);
- }
- cout << "Left = "+to_string(left) +", Right = "+to_string(right)+" gave "+to_string(theset.size())+" permutations" << endl;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement