-
Notifications
You must be signed in to change notification settings - Fork 0
/
peng.cpp
68 lines (64 loc) · 1.18 KB
/
peng.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
#include <bits/stdc++.h>
using namespace std;
void printResult(vector<vector<int> >& result)
{
for (int i = 0; i<result.size(); i++) {
cout << "{";
for (int j = 0; j<result[i].size(); j++) {
cout << " " << result[i][j];
}
cout << " }" << endl;
}
}
void printArray(vector<int>& v) {
cout << "{";
for (int i = 0; i<v.size(); i++) {
cout << " " << v[i];
}
cout << " }" << endl;
}
string decode(const string& str)
{
stack<int> counts;
stack<string> prefixs;
string res;
for (size_t i = 0; i < str.length(); i++)
{
if (str[i] >= '0' && str[i] <= '9') {
int tmpcount = 0;
while (i < str.length() && str[i] >= '0' && str[i] <= '9') {
tmpcount = tmpcount * 10 + (str[i] - '0');
i++;
}
counts.push(tmpcount);
}
else if (str[i] == '[') {
prefixs.push(res);
res = "";
}
else if (str[i] == ']') {
int c = counts.top();
counts.pop();
string pre = prefixs.top();
prefixs.pop();
for (int i = 0; i < c; i++) {
pre.append(res);
}
res = pre;
}
else
res.append(1, str[i]);
}
return res;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
string s = "";
while (cin>>s)
{
cout << decode(s) << endl;
}
return 0;
}