-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathduval.cpp
More file actions
43 lines (33 loc) · 768 Bytes
/
duval.cpp
File metadata and controls
43 lines (33 loc) · 768 Bytes
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
#include <bits/stdc++.h>
using namespace std;
/*
Build: O(n)
*/
vector<string> duval(string s){
vector<string>res;
int n = (int)s.size();
int i = 0; // s1 => [0...i-1] processed
int j = 0; // s2 => pre-simple string
int k = 1; // s3 => [k...n] unprocessed
while(i < n){
while (k < n && s[j] <= s[k]){
if (s[j] == s[k]) j++;
else j = i; // new simple created
k++;
}
// getting all simple string from s2
while(i <= j){
res.push_back(s.substr(i, k-j));
i += k-j;
}
j = i;
k = i+1;
}
return res;
}
int main(){
string s = "aababbaaab";
for(string &t:duval(s))
cout << t << endl;
return 0;
}