-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF747E.cpp
86 lines (85 loc) · 1.89 KB
/
CF747E.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
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
#include <iostream>
#include <cstdio>
#include <string.h>
#include <sstream>
#include <deque>
#include <cmath>
#include <vector>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#define MAXN 100010
#define FL(i,j,k) for(int i=j;i<k;++i)
typedef long long int ll;
using namespace std;
struct node{
vector<int> ch;
string s;
int d=0,num;
};
stack<int> st;
string str;
int i;
pair<string, int> eat(){
int chi=0,bi=1;
while (str[i]>='0' && str[i]<='9') {
chi+=(str[i]-'0')*bi;
bi*=10;
--i;
}
int stay=i;
--i;
while (str[i]!=','&&i>=0)--i;
string f;
FL(j,i+1, stay)f.push_back(str[j]);
return pair<string, int>(f,chi);
}
vector<vector<pair<string,int>>> vvs;
node nd[500000];
int main(){
getline(cin, str);
i=str.size();
int ndnum=0;
while (i>=0) {
--i;
if(i<0)break;
pair<string, int> Q=eat();
nd[ndnum].s=Q.first;
nd[ndnum].num=i;
while (Q.second>0) {
nd[ndnum].ch.push_back(st.top());
st.pop();
Q.second--;
}
st.push(ndnum++);
}
vvs.resize(500000);
while (!st.empty()) {
int p=st.top();st.pop();
queue<int> q;q.push(p);
while (!q.empty()) {
node p=nd[q.front()];
vvs[p.d].push_back(pair<string,int>(p.s,p.num));
q.pop();
for(int i=0;i<p.ch.size();++i){
nd[p.ch[i]].d=p.d+1;
q.push(p.ch[i]);
}
}
}
int k=0;
while (0!=vvs[k].size()) {
sort(vvs[k].begin(), vvs[k].end(), [=](pair<string,int> A,pair<string,int> B){return A.second<B.second;});
++k;
}
printf("%d\n",k);
k=0;
while (0!=vvs[k].size()) {
for(pair<string,int> s : vvs[k]){
printf("%s ",s.first.c_str());
}
printf("\n");
++k;
}
}