第一次写的时候,思路有点乱,代码写的乱七八糟,后面看了大牛的题解,才恍然大悟!!
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
string str,str2;
stack<char>pq;
cin>>str;
for(int i=0;i<str.length();i++)
{
if(str[i]=='('||isdigit(str[i]))
pq.push(str[i]);
else if(isalpha(str[i]))
{
int n;
if(isdigit(pq.top()))
{
n=pq.top()-'0';
pq.pop();
while(n--)
pq.push(str[i]);
}
else
pq.push(str[i]);
}
else if(str[i]==')')
{
string str1;
int n;
while(pq.top()!='(')
{
str1.insert(str1.begin(),pq.top());
pq.pop();
}
pq.pop();
if(isdigit(pq.top())&&!pq.empty())
{
n=pq.top()-'0';
pq.pop();
}
else
n=1;
while(n--)
{
for(int j=0;j<str1.length();j++)
pq.push(str1[j]);
}
}
}
while(!pq.empty())
{
str2.insert(str2.begin(),pq.top());
pq.pop();
}
cout<<str2<<"\n";
}
return 0;
}