AcWing 865. 字符串展开
原题链接
简单
作者:
wzc118
,
2020-03-08 14:39:08
,
所有人可见
,
阅读 722
stack
//字符串展开
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin >> s;
stack<string> st;
stack<int> cnt;
string res;
int num = 0;
for (const auto c:s){
if (c == '%'){
cnt.push(num);
num = 0;
st.push(res);
res.clear();
} else if (c >= '0' && c <= '9'){
num *= 10;
num += c - '0';
} else if (c == '#'){
const auto count = cnt.top();
auto t = st.empty() ? "" : st.top();
for (int i = 0; i < count ; ++i)
t += res;
if (!st.empty()) st.pop();
cnt.pop();
res = t;
} else {
res += c;
}
}
cout << res << endl;
}