Advertisement
Guest User

aaaaaaaaaaaaaaaaaaaa

a guest
Dec 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_set>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. #include <string>
  7. #include <sstream>
  8.  
  9. template <typename T>
  10. std::string to_string(T value)
  11. {
  12.     std::ostringstream os;
  13.     os << value;
  14.     return os.str();
  15. }
  16.  
  17. string rotateNFromLeft(string st, int n)
  18. {
  19.     return st.substr(n-1,1) + st.substr(0,n-1) + st.substr(n);
  20. }
  21.  
  22. string rotateNFromRight(string st, int n)
  23. {
  24.     int size = st.length();
  25.     return st.substr(0, size-n) + st.substr(size-1) + st.substr(size-n, n-1);
  26. }
  27.  
  28. int main(int argc, char *argv[]) {
  29.     cout << "Please enter the number of items to be permuted--> ";
  30.     int i;
  31.     cin >> i;
  32.     cout << "Testing permutations of " + to_string(i) + " elements" << endl;
  33.     for (int left = 1; left <= i; left++)
  34.     {
  35.         for (int right = 1; right <= left; right++)
  36.         {
  37.             if (left+right > i)
  38.             {
  39.                 string nums = "";
  40.                 char ch = 'a';
  41.                 for (int x=1; x<=i; x++)
  42.                 {
  43.                     nums += ch;
  44.                     ch++;
  45.                 }
  46.                 // Create the "unordered set" into which the strings will be placed
  47.                 unordered_set<string> theset;
  48.                 theset.insert(nums);
  49.                 // Create the stack which will contain all the ones yet to be tested
  50.                 stack<string> thestack;
  51.                 thestack.push(nums);
  52.                
  53.                 // Loop until there is nothing left in the stack to test
  54.                 while (thestack.size() > 0)
  55.                 {
  56.                     string nexttotest = thestack.top();
  57.                     thestack.pop();
  58.                     string leftrotate = rotateNFromLeft(nexttotest, left);
  59.                     if (theset.insert(leftrotate).second == true)
  60.                         thestack.push(leftrotate);
  61.                     string rightrotate = rotateNFromRight(nexttotest, right);
  62.                     if (theset.insert(rightrotate).second == true)
  63.                         thestack.push(rightrotate);
  64.                 }
  65.                 cout << "Left = "+to_string(left) +", Right = "+to_string(right)+" gave "+to_string(theset.size())+" permutations" << endl;
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement