/*
题意:根据后缀表达式,输出使用队列实现相同效果的序列
需要用到栈、队列、二叉树
*/
//无指针,推荐,可是未通过!!!
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
const int kMax=10007;
struct Node
{
int parent,left,right;
}tree[kMax];
int ans[kMax];
int main()
{
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
//*/
int T;
scanf("%d",&T);
while(T--)
{
stack<int> s;
memset(tree,-1,sizeof(tree));
string line;
cin>>line;
for(int i=0;i<line.size();i++)
{
if(islower(line[i]))
s.push(i);
else
{
int x,y;
y=s.top();
s.pop();
x=s.top();
s.pop();
tree[i].left=x;
tree[i].right=y;
tree[x].parent=tree[y].parent=i;
s.push(i);
}
}
int root;
for(int i=0;i<line.size();i++)
{
if(tree[i].parent==-1)
{
root=i;
break;
}
}
queue<int> q;
q.push(root);
int u=0;
memset(ans,0,sizeof(ans));
while(!q.empty())
{
int x=q.front();
q.pop();
ans[u++]=x;
if(tree[x].left!=-1)
q.push(tree[x].left);
if(tree[x].right!=-1)
q.push(tree[x].right);
}
for(int i=u-1;i>=0;i--)
printf("%c",line[ans[i]]);
printf("\n");
}
return 0;
}
//指针实现,通过
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
const int kMax=10007;
char ans[kMax];
struct TNode
{
char op;
TNode *left,*right;
};
TNode *creatTree(char a=0,TNode *lchild=NULL,TNode *rchild=NULL)
{
TNode *tree=new TNode;
tree->left=lchild;
tree->right=rchild;
tree->op=a;
return tree;
}
int main()
{
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
//*/
int T;
scanf("%d",&T);
while(T--)
{
stack<TNode *> s;
string line;
cin>>line;
for(int i=0;i<line.size();i++)
{
if(islower(line[i]))
{
TNode *tree=creatTree(line[i]);
s.push(tree);
}
else
{
TNode *l_child,*r_child;
r_child=s.top();
s.pop();
l_child=s.top();
s.pop();
TNode *tree=creatTree(line[i],l_child,r_child);
s.push(tree);
}
}
TNode *root=s.top();
queue<TNode *> q;
q.push(root);
int u=0;
memset(ans,0,sizeof(ans));
while(!q.empty())
{
TNode *tree=q.front();
q.pop();
ans[u++]=tree->op;
if(tree->left!=NULL)
q.push(tree->left);
if(tree->right!=NULL)
q.push(tree->right);
}
for(int i=u-1;i>=0;i--)
printf("%c",ans[i]);
printf("\n");
}
return 0;
}
第二次做:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int nMax=10000+100;
char line[nMax];
struct Node
{
char c;
struct Node *lchild,*rchild;
};
Node *stack[nMax];
int top;
Node *build_tree()
{
int len=strlen(line);
for(int i=0;i<len;i++)
{
if(line[i]>='a' && line[i]<='z')
{
stack[++top]=(Node *)malloc(sizeof(Node));
stack[top]->c=line[i];
stack[top]->lchild=stack[top]->rchild=0;
}
else if(line[i]>='A' && line[i]<='Z')
{
Node *a=stack[top--];
Node *b=stack[top--];
Node *d=(Node *)malloc(sizeof(Node));
d->c=line[i];
d->lchild=b;
d->rchild=a;
stack[++top]=d;
}
}
return stack[0];
}
void print(Node *root)
{
/*if(root==NULL) return;
print(root->lchild);
print(root->rchild);
printf("%c",root->c);*/
Node *queue[nMax];
int front,rear;
front=rear=-1;
queue[++front]=root;
char ans[nMax];
int m=0;
while(rear!=front)
{
Node *q=queue[++rear];
ans[m++]=q->c;
if(q->lchild)
queue[++front]=q->lchild;
if(q->rchild)
queue[++front]=q->rchild;
}
for(int i=m-1;i>=0;i--)
printf("%c",ans[i]);
}
void delet(Node *root)
{
if(root==NULL) return;
delet(root->lchild);
delet(root->rchild);
free(root);
}
int main()
{
//freopen("f://data.in","r",stdin);
int T;
scanf("%d\n",&T);
while(T--)
{
top=-1;
gets(line);
Node *root=build_tree();
print(root);
printf("\n");
delet(root);
}
return 0;
}