原来这题要用树……看了别人的解题报告才明白
#include<stdio.h>
#include<string.h>
#include<math.h>
struct tree
{
tree *left,*right;
char op;
}*stack[10010],*queue[10010];
char s[10010];
tree *creat1(char c)
{
tree *nt=new tree;
nt->left=NULL;
nt->right=NULL;
nt->op=c;
return nt;
}
tree *creat2(char c,tree *t1,tree *t2)
{
tree *nt=new tree;
nt->left=t1;
nt->right=t2;
nt->op=c;
return nt;
}
void solve(tree *pt)
{
int front=0,rear=0,cnt=0,i;
char ans[10010];
tree *tmp;
queue[rear++]=pt;
while(front<rear)
{
tmp=queue[front++];
if(tmp->left!=NULL)
queue[rear++]=tmp->left;
if(tmp->left!=NULL)
queue[rear++]=tmp->right;
ans[cnt++]=tmp->op;
}
for(i=cnt-1;i>=0;i--)
printf("%c",ans[i]);
printf("\n");
}
int main()
{
int t,len,i,top;
tree *tmp1,*tmp2;
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);
top=0;
len=strlen(s);
for(i=0;i<len;i++)
if('a'<=s[i]&&s[i]<='z')
stack[top++]=creat1(s[i]);
else
{
tmp1=stack[--top];
tmp2=stack[--top];
stack[top++]=creat2(s[i],tmp2,tmp1);
}
solve(stack[0]);
}
return 0;
}