E. Comments
链接:
http://codeforces.com/contest/747/problem/E
代码:
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 #include <cstring> 10 #include <sstream> 11 #include <iostream> 12 #include <algorithm> 13 #include <functional> 14 using namespace std; 15 #define rep(i,a,n) for (int i=a;i<=n;i++) 16 #define per(i,a,n) for (int i=n;i>=a;i--) 17 #define pb push_back 18 #define mp make_pair 19 #define all(x) (x).begin(),(x).end() 20 #define fi first 21 #define se second 22 #define SZ(x) ((int)(x).size()) 23 typedef vector<int> VI; 24 typedef long long ll; 25 typedef pair<int, int> PII; 26 const ll mod = 1e9 + 7; 27 const int inf = 0x3f3f3f3f; 28 const double eps = 1e-7; 29 // head 30 31 const int MAXN = 1000005; 32 vector<pair<string, int> >v; 33 vector<string>res[MAXN]; 34 35 int main() 36 { 37 ios::sync_with_stdio(false); 38 cin.tie(NULL); 39 string str; 40 cin >> str; 41 for (int i = 0; i<(int)str.size(); i++) 42 if (str[i] == ',')str[i] = ' '; 43 stringstream ss; 44 ss.str(str); 45 while (ss >> str) 46 { 47 int tmp; 48 ss >> tmp; 49 v.push_back(make_pair(str, tmp)); 50 } 51 stack<int>st; 52 st.push(-1); 53 for (int i = 0, j = 1; i<(int)v.size(); i++) 54 { 55 while (st.top() == 0)j--, st.pop(); 56 res[j].push_back(v[i].first); 57 st.top()--; 58 st.push(v[i].second), j++; 59 } 60 int mx = 0; 61 for (int i = 1; i<MAXN && !res[i].empty(); i++) 62 mx = i; 63 cout << mx << endl; 64 for (int i = 1; i <= mx; i++) 65 { 66 for (int j = 0; j<(int)res[i].size(); j++) 67 cout << res[i][j] << " "; 68 cout << endl; 69 } 70 return 0; 71 }