-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode String.cpp
More file actions
109 lines (98 loc) · 1.45 KB
/
Copy pathDecode String.cpp
File metadata and controls
109 lines (98 loc) · 1.45 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
#include <cstdio>
#include <iostream>
#include <cstring>
#include <sstream>
#include <stack>
#include <cctype>
using namespace std;
const int MAXN = 3;
string decode(string s)
{
stack<string> st;
string cut = "", cur = "";
s += '$';
int len = s.length();
bool d = false, al = false;
string c;
for(int i = 0; i < len; i++)
{
if(s[i] != ']' && s[i] != '$') // in stack
{
if(s[i] == '[')
{
if(cut != "")
st.push(cut);
cut = "";
st.push("[");
d = al = false;
continue;
}
else if(isdigit(s[i]) && !d)
{
if(cut != "")
st.push(cut);
cut = "";
d = true;
al = false;
}
else if(!isdigit(s[i]) && !al)
{
if(cut != "")
st.push(cut);
cut = "";
al = true;
d = false;
}
cut += s[i];
}
else
{
if(cut != "")
{
st.push(cut);
cut = "";
}
d = al = false;
while(!st.empty())
{
c = st.top();
st.pop();
if(c[0] == '[')
continue;
if(isdigit(c[0]))
{
int num = 0;
string temp = "";
stringstream ss;
ss << c;
ss >> num;
while(num --)
temp = temp + cur;
cur = temp;
break;
}
else
{
cur = c + cur;
}
}
if(s[i] == '$')
return cur;
else
{
st.push(cur);
cur = "";
}
}
}
return "";
}
int main()
{
string s[] = {"sd2[f2[e]g]i", "3[a]2[bc]", "3[a2[c]]"};
for(int i = 0; i < MAXN; i++)
{
cout << decode(s[i]) << endl;
}
return 0;
}