不太会用递归的说。。不过一把过了,纪念一下。。代码太水,欢迎批评指正
#include <iostream>
#include <string>
using namespace std;
bool isNum(char ch)
{
return (ch <= '9'&&ch>='0');
}
bool isLetter(char ch)
{
return (ch <= 'z'&&ch>='a');
}
string outSpread(string str)
{
string rslt = "";
int i = 0 ;
while ( i < str.size() )
{
int tmp = 0 ;
if ( isNum(str[i]) )
{
//get the counts of next string
tmp += str[i]-'0';
while( i<str.size()-1 && isNum((str[++i])) )
tmp = tmp*10 + str[i]-'0';
if( i<str.size() && isLetter(str[i]) )
{
for(int j=0; j<tmp; j++)
rslt += str[i];
i++;
}
else if( i<str.size() && str[i]=='(' )
{
i++;
int match = 1;
string rec = "";
while( i<str.size())
{
if(str[i]=='(')match++;
else if(str[i]==')')
{
match -- ;
if(match==0)
{
i++;
break;
}
}
rec += str[i];
i++;
}
rec = outSpread(rec);
for(int j=0; j<tmp; j++)
rslt += rec;
}
}
else if(isLetter(str[i]))
{
rslt += str[i];
i++;
}
else if(str[i]=='(')
{
i++;
int match = 1;
string rec = "";
while( i<str.size())
{
if(str[i]=='(')match++;
else if(str[i]==')')
{
match -- ;
if(match==0)
{
i++;
break;
}
}
rec += str[i];
i++;
}
rec = outSpread(rec);
for(int j=0; j<tmp; j++)
rslt += rec;
}
}
return rslt;
}
int main()
{
int num;
cin >> num;
string tmp;
while(num--)
{
cin >> tmp;
cout << outSpread(tmp)<<endl;
}
return 0;
}