题目链接:【hdu 1274展开字符串】
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
char str[300], si[2500000], sl[2500000];
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
stack<char>s;
scanf("%s", str);
int len=strlen(str);
for(int i=0; i<len; i++)
{
if(str[i]==')')
{
int li=0;
while(!s.empty())
{
char c=s.top();
s.pop();
if(c=='(') break;
si[li++]=c;
}
int ci=0, di=1;
while(!s.empty()&&s.top()>='0'&&s.top()<='9')
{
ci=ci+(s.top()-'0')*di;
s.pop();
di*=10;
}
if(ci==0) ci=1;
while(ci--)
{
for(int j=li-1; j>=0; j--)
{
s.push(si[j]);
}
}
}
else
{
if((str[i]>='0'&&str[i]<='9')||str[i]=='(') s.push(str[i]);
else
{
if(s.empty()) s.push(str[i]);
else if(s.top()>='0'&&s.top()<='9')
{
int d=0,li=1;
while(!s.empty() && s.top()>='0' && s.top()<='9')
{
d = d+(s.top()-'0')*li;
s.pop();
li*=10;
}
while(d--)
{
s.push(str[i]);
}
}
else s.push(str[i]);
}
}
}
int li=0;
while(!s.empty())
{
si[li++]=s.top();
s.pop();
}
memset(sl,'\0',sizeof(sl));
int lk=0;
for(int i=li-1; i>=0; i--)
{
sl[lk++]=si[i];
}
printf("%s\n", sl);
}
return 0;
}