-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexicographic.cc
61 lines (53 loc) · 1.33 KB
/
lexicographic.cc
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
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool myFunction(int i, int j){return (i<j);}
bool myfunction(int i, int j){return (i>j);}
char firstCharacter(string s,int *indexOfFirst);
char secondCharacter(string s,char firstChar, int *indexOfSecond);
int main(){
string s = "HELLO";
sort(s.begin(),s.end(),myfunction);
string reverse = s;
sort(s.begin(),s.end());
int count = 1;
cout<<s<<"\n";
while(s!=reverse){
int indexOfFirst;
char first = firstCharacter(s,&indexOfFirst);
if(first == 'Z'){
cout<<"broken";
break;
}
string newString = s.substr(indexOfFirst + 1);
int indexOfSecond;
char second = secondCharacter(newString,first,&indexOfSecond);
swap(s[indexOfFirst],s[indexOfFirst + 1 + indexOfSecond]);
sort(s.begin() + indexOfFirst + 1,s.end());
cout<<s<<"\n";
}
}
char firstCharacter(string str,int *indexOfFirst){
int i = str.length() - 1;
for(;i >= 1 ; i--){
int one = str[i-1];
int two = str[i];
if(myFunction(one,two)){
*indexOfFirst = i-1;
return one;
}
}
return 'Z';
}
char secondCharacter(string s, char firstChar,int *indexOfSecond){
string::iterator it;
*indexOfSecond = 0;
for(it = s.begin();it!=s.end();++it){
if(*it > firstChar && *it < s[*indexOfSecond] ){
*indexOfSecond = it - s.begin();
}
}
return s[*indexOfSecond];
}