看到括号就想到了栈什么的,这题可以用递归展开括号,不需要栈.
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string unfold(){
string ret,tmp;
char ch;
int times = 1;
while ((ch = getchar()) != ')' && ch != '\n'){
if(isdigit(ch)){
cin.putback(ch);
scanf("%d", ×);
}else{
if(ch == '('){
tmp = unfold();
for (int i = 0; i < times;++i){
ret +=tmp;
}
times = 1;
}else{
ret += string(times,ch);
times = 1;
}
}
}
return ret;
}
int main(){
int T;
scanf("%d\n", &T);
while (T--){
printf("%s\n",unfold().c_str());
}
return 0;
}