forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI.cpp
More file actions
132 lines (111 loc) · 3.14 KB
/
I.cpp
File metadata and controls
132 lines (111 loc) · 3.14 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
string s, tmp;
int x[1011][1011];
char a[3011][3011];
int nCell;
int getNum(const string& s, int i) {
int from = i + 1;
while (s[from] != '"') ++from;
int to = from + 1;
while (s[to] != '"') ++to;
int res = 0;
FOR(x,from+1,to-1)
res = res * 10 + s[x] - '0';
return res;
}
void parseCell(int row, string s) {
++nCell;
int nRow = 1, nCol = 1;
REP(i,SZ(s))
if (s[i] == 'c') {
nCol = getNum(s, i);
}
else if (s[i] == 'r') {
nRow = getNum(s, i);
}
else if (s[i] == '>') break;
int startCol = 1;
while (x[row][startCol]) ++startCol;
FOR(i,row,row+nRow-1)
FOR(j,startCol,startCol+nCol-1) {
if (x[i][j]) throw 1;
x[i][j] = nCell;
}
int i1 = row * 2 - 1;
int i2 = (row + nRow - 1) * 2 + 1;
int j1 = startCol * 2 - 1;
int j2 = (startCol + nCol - 1) * 2 + 1;
FOR(j,j1,j2) {
if (a[i1][j] == ' ') a[i1][j] = '-';
if (a[i2][j] == ' ') a[i2][j] = '-';
}
FOR(i,i1,i2) {
if (a[i][j1] == ' ') a[i][j1] = '|';
if (a[i][j2] == ' ') a[i][j2] = '|';
}
a[i1][j1] = a[i1][j2] = a[i2][j1] = a[i2][j2] = '+';
// cout << i1 << ' ' << j1 << ' ' << i2 << ' ' << j2 << endl;
}
void parseRow(int row, string s) {
int start = -1;
REP(i,SZ(s)-4) {
if (s.substr(i, 3) == "<td") {
start = i;
}
else if (s.substr(i, 5) == "</td>") {
parseCell(row, s.substr(start, i - start));
}
}
}
void parseTable(string s) {
int row = 0;
int start = -1;
REP(i,SZ(s)-4) {
if (s.substr(i, 3) == "<tr") {
start = i;
}
else if (s.substr(i, 5) == "</tr>") {
++row;
parseRow(row, s.substr(start, i - start));
}
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("table.in", "r", stdin);
freopen("table.out", "w", stdout);
while (getline(cin, tmp)) {
REP(i,SZ(tmp))
if (tmp[i] >= 'A' && tmp[i] <= 'Z') {
tmp[i] ^= ' ';
}
s += tmp;
}
memset(a, ' ', sizeof a);
try {
parseTable(s);
} catch (int e) {
cout << "ERROR" << endl;
return 0;
}
int nRow = 0;
FOR(i,1,3000) FOR(j,1,3000) {
if (a[i][j] != ' ') nRow = i;
}
FOR(i,1,nRow) {
int nCol = 0;
FOR(j,1,3000) if (a[i][j] != ' ') nCol = j;
FOR(j,1,nCol) cout << a[i][j];
cout << endl;
}
}