-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathD.cpp
88 lines (67 loc) · 2 KB
/
D.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
87
88
#include <bits/stdc++.h>
typedef int ll;
#define repVector(v) for( auto it = v.begin(); it != v.last(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = (ll)(a); i <= (ll)(b); i++ )
#define ROF(i,a,b) for( ll i = (ll)(a); i >= (ll)(b); i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl;
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define INPFILE freopen("input.in","r",stdin)
#define OUTFILE freopen("output.out","w",stdout)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
int main() {
BOOST;
ll n; cin >> n;
vector<string> v;
string ans[n];
ll row = 0;
FOR(i,1,n) {
string s; cin >> s;
v.pb(s);
row = max(row,(ll)s.size());
}
vector<pair<char,ll>> Q[row];
FOR(i,0,n-1) FOR(j,0,v[i].size()-1) {
Q[j].pb({v[i][j],i});
if( i+1 < n && j >= (ll)v[i+1].size() )
Q[j].pb({'^',i+1});
}
vector<bool> isP(n,1), br(n,0);
FOR(j,0,row-1) {
stack< pair<char,ll> > st;
vector<ll> newbr;
for( auto p : Q[j] ) {
ll i = p.S; char c = p.F;
if( isP[i] && !st.empty() && c > st.top().F && !br[i] ) newbr.pb(i);
while( br[i] && !st.empty()) {
char c = st.top().F;
int ptr = st.top().S;
if( isP[ptr] && c != '^' )
ans[ptr].pb(c);
st.pop();
}
if(!isP[i]) continue;
char top = 0;
if(!st.empty()) top = st.top().F;
while(!st.empty() && top > c) {
isP[st.top().S] = 0;
st.pop();
}
st.push({c,i});
}
while(!st.empty()) {
char c = st.top().F;
int ptr = st.top().S;
if( isP[ptr] && c != '^' )
ans[ptr].pb(c);
st.pop();
}
for( ll u : newbr ) br[u] = 1;
}
FOR(i,0,n-1) cout << ans[i] << '\n';
}