RT
给出了一个后缀表达式 要给出用队列表示时的答案 看simple output 知道输出时按照数的层数 从下到上从右到左一一输出的
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<stack>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int maxn = 10005;
typedef struct TNode
{
char v;
struct TNode *l,*r;//左节点右节点
}Node;
stack<Node*> st;
queue<Node*> q;
Node* newnode()
{
Node* u = (Node*)malloc(sizeof(Node));
if(u != NULL)
u->l = u->r = NULL;
return u;
}
void addnode(char* s)
{
while(!st.empty())st.pop();
Node* u;
for(int i = 0; i < strlen(s); i++)
{
if(islower(s[i]))
{
u = newnode();
u->v = s[i];
st.push(u);
}
else
{
u = newnode();
u->r = st.top();
st.pop();
u->l = st.top();
st.pop();
u->v = s[i];
st.push(u);
}
}
}
int n = 0;
void BFS()//层次遍历
{
while(!q.empty())q.pop();
char p[maxn];
memset(p,0,sizeof(p));
q.push(st.top());
while(!q.empty())
{
Node* u = q.front();
q.pop();
p[n++] = u->v;
if(u->l != NULL) q.push(u->l);
if(u->r != NULL) q.push(u->r);
}
for(int i = n-1; i >= 0;i --)
printf("%c",p[i]);
printf("\n");
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t;
scanf("%d",&t);
while(t--)
{
char s[maxn];
scanf("%s",s);
n = 0;
addnode(s);
BFS();
}
return 0;
}