class Solution {
public:
string compress(string str) {
stack<char> st;
for (int i = 0; i < str.size(); i++) {
//HG[3|B[2|CA]]F
if (str[i] == ']') {
string tmp;//CA
while (!st.empty() && st.top() != '|') {
tmp = st.top() + tmp;
st.pop();
}
st.pop();//pop()'|'
string s1 = "";//s1代表tmp的叠加次数
while (!st.empty() && st.top() != '[') {
s1 = st.top() + s1;
st.pop();
}
st.pop();//pop'['
int t1 = stoi(s1);
string s2 = "";//s2代表哦解压缩后重新压入栈中的srting
while (t1) {
s2 += tmp;
t1--;
}
for (int j = 0; j < s2.size(); j++)
st.push(s2[j]);
}else{
st.push(str[i]);
}
}
string ans = "";
while (!st.empty()) {
ans = st.top() + ans;
st.pop();
}
return ans;
}
};