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
|
#include<iostream>
#include<vector>
using namespace std;
void Reverse_Tail(vector<char>& List,int k);
vector<char> Swap(vector<char> InputList,int k, int l);
vector<char> GetInput();
int Find_K(vector<char> List);
int Find_l(vector<char> List,int k);
int main(){
vector<char> Test2,Test = GetInput();
int K = Find_K(Test);
int l = Find_l(Test,K);
for(int i=0;i<Test.size();i++){
cout << Test[i] << " " ;
}
cout << endl << "K = " << K << endl<< "l = " << l << endl<< endl;
Test2 = Swap(Test,K,l);
//Ran the next for loop spit vector before the Reverse_Tail function and it does display normally
for(int i=0;i<Test2.size();i++){
cout << Test2[i] << " " ;
}
cout << endl<< endl;
Reverse_Tail(Test2,K);
// now when i run the same loop again it displays incorrectly
for(int i=0;i<Test2.size();i++){
cout << Test2[i] << " " ;
}
cout << endl<< endl;
return 0;
}
void Reverse_Tail(vector<char>& List,int k){
int i = k+1;
int lim = (List.size() - i)/2;
int len = List.size()-1;
while(i < List.size() - lim){
List = Swap(List,i,len);
len--;
i++;
}
}
vector<char> Swap(vector<char> InputList,int k, int l){
vector<char> OutList = InputList;
int Temp = OutList[l];
OutList[l] = InputList[k];
OutList[k] = Temp;
return OutList;
}
int Find_l(vector<char> List,int k){
int l=List.size()-1;
while(List[l] < List[k]){
l--;
}
return l;
}
//Remeber k is an index
int Find_K(vector<char> List){
int k = List.size()-2;
while(List[k] > List[k+1]){
k--;
}
if(k == List.size()-1){
return -1;
}
return k;
}
vector<char> GetInput(){
vector<char> InputString;
cout << "Please input the string of symbols you would like to gain all permutations of: ";
char temp = 0 ;
while( temp != '\n' ){
cin.get(temp);
InputString.push_back(temp);
}
return InputString;
}
|