题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1274
意思很明确,开始想到类似于表达式中缀转后缀,画了半天每个结果,突然意识到完全不用,用递归就可以了:
火星十一郎的这个写的比较简洁:
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<string>
using namespace std;
typedef long long ll;
char data[300];
//stack<char>sta;
int len;
int func(int index)
{
int count;
int e;
char c;
for(c=data[index++];index<len && c!=')';c=data[index++])
{
for(count=0;isdigit(c);c=data[index++])
count=count*10+c-'0';
if(!count)
count=1;
if(c=='(')
{//递归下一层
while(count--)
e=func(index);
index=e;
}
else
{
while(count--)
putchar(c);
}
}
if(c==')')
return index;
}
int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%s",data);
len=strlen(data);
func(0);
printf("\n");
}
}
return 0;
}