题目链接:http://acm.sdibt.edu.cn:8080/judge/contest/view.action?cid=390#problem/H

悲催的这道题,想了好几天,不知道怎么做,没办法看了高人的思路,原来用到表达式树的按层次遍历!下面是我编的代码:

#include <stdio.h>
#include <string.h>
typedef struct nod
{
char word;
struct nod *right;
struct nod *left;
}node;
node *stack[10000],*head,*p,*quere[100000];
node word[10000];
char t[10000];
int bottom=0,top=0,base=0,top2=0,n;
void print()
{
int k=0;
quere[top2++]=head;
while(top2>base)
{
t[k++]=quere[base]->word;
if(quere[base]->right!=NULL)
quere[top2++]=quere[base]->right;
if(quere[base]->left!=NULL)
quere[top2++]=quere[base]->left;
base++;
}
while(n–)
printf(“%c”,t[n]);
}
int main()
{
int T,i,j;
scanf(“%d”,&T);
getchar();
for(i=0;i<T;i++)
{
top=0;
top2=0;
base=0;
memset(t,0,sizeof(char)*10000);
gets(t);
n=strlen(t);
j=0;
while(j<n)
{
word[j].word=t[j];
word[j].right=NULL;
word[j++].left=NULL;
}
for(j=0;j<n;j++)
{
if(word[j].word>=’a’&&word[j].word<=’z’)
stack[top++]=&word[j];
else
{
word[j].left=stack[–top];
word[j].right=stack[–top];
stack[top++]=&word[j];
}
}
head=stack[–top];
print();
printf(“\n”);
}
return 0;
}

发表评论

电子邮件地址不会被公开。

Post Navigation