How can I optimise this

I'm sure my order checking for loops can be optimised here...

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
#include <string>
#include <iostream>
#include <vector>
using namespace std;

string globStr;
vector<string>subStrings;

void permutateTheStringsRecur(string str, string res) {
	if (res != ""){
		cerr << "res: " << res << endl;
		bool inOrder = true;
		int last = 0;
		for (int i = 0; i < globStr.length(); i++){
			for (int a = 0; a < res.length(); a++){
				if (res[a] == globStr[i]){
					if (a < last){
						inOrder = false;
					}
					last = a;
				}
			}
		}
		if (inOrder){
			cerr << "IN ORDER!" << endl;
			subStrings.push_back(res);
		}
	}


	for (int i = 0; i < str.length(); i++){
		permutateTheStringsRecur(string(str).erase(i, 1), res + str[i]);
	}
}
int main() {
	string str;
	cin >> str;
	globStr = str;
	permutateTheStringsRecur(str, "");
	int alternatingSubStrings = 0;
	cerr << "size of substrings: " << subStrings.size() << endl;
	for (int i = 0; i < subStrings.size(); i++){
		cerr << "Processing substring: " << subStrings[i] << endl;
		bool inOrder = true;
		string subStr = subStrings[i];
		int randomInt = 0;
		for (int a = 0; a < subStr.length(); a++){
			int cur = subStr[a];
			//cerr << "cur: " << cur << endl;
			int next = a + 1;
			if (next < subStr.length()){
				int nextCur = subStr[next];
				if (cur > nextCur && randomInt == 0){
					inOrder = false;
				}
				else if (cur < nextCur && randomInt == 1){
					inOrder = false;
				}
				if (randomInt == 0){
					randomInt = 1;
				}
				else{
					randomInt = 0;
				}
			}
			else{
				break;
			}
		}
		if (inOrder){
			cerr << "SUBSTRING IN ORDER" << endl;
			alternatingSubStrings++;
		}
	}
	cout << alternatingSubStrings % 1000000007;
	return 0;
}  

Basically I have to find all the permutations of a string but I cannot change the order, so abcde can be abde or abc or ae but not aedb

Then I have to check if the substring follows this pattern, first letter ascii value < second letter ascii value. second letter ascii value > third letter ascii value. third letter ascii value < fourth ascii value.

So, how would I optimise it
Topic archived. No new replies allowed.