-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comments.cpp
60 lines (56 loc) · 1.62 KB
/
Comments.cpp
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
#include <iostream>
#include <vector>
#include <sstream>
#include <stack>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string input;
cin >> input;
stringstream stream(input);
string segment;
vector<string> words;
while (getline(stream, segment, ',')) {
words.push_back(segment);
}
int curDepth = 0;
vector<vector<string> > depths;
int maxDepth = 0;
stack<pair<int, int> > parent;
for (int i = 0; i < words.size(); i++) {
string comment = words[i];
i++;
long long children = stoll(words[i]);
while(!parent.empty() && parent.top().second == 0){
parent.pop();
}
if (parent.empty()) {
curDepth = 0;
} else {
pair<int, int> topVal = parent.top();
parent.pop();
parent.push({topVal.first, topVal.second - 1});
curDepth = topVal.first + 1;
maxDepth = max(curDepth, maxDepth);
}
if (depths.size() <= curDepth) {
depths.push_back({comment});
} else {
depths[curDepth].push_back(comment);
}
//cout << curDepth << " " << words[i-1] << endl;
parent.push({curDepth, children});
}
cout << maxDepth+1 << endl;
for (int i = 0; i <= maxDepth; i++) {
if (depths[i].size()) {
cout << depths[i][0];
for (int j = 1; j < depths[i].size(); j++) {
cout << " " << depths[i][j];
}
cout << endl;
}
}
return 0;
}