来源:http://acm.hdu.edu.cn/showproblem.php?pid=1872
归类: 图论、二叉搜索树
3 aa 10 bb 10 cc 20 cc 20 bb 10 aa 10 3 aa 10 bb 10 cc 20 cc 20 aa 10 bb 10 3 aa 10 bb 10 cc 20 aa 10 bb 10 cc 20
Not Stable cc 20 aa 10 bb 10 Right Error cc 20 aa 10 bb 10
题解: 第一次用树的概念做题~~ 先将输入按照大小存储在二叉树中: 右子树大于根节点,根节点大于左子树。之后再与题目中的数据进行匹配判断,相应的输出结果即可。
AC代码:
#include<cstdio>
#include<cstring>
struct Node{
char name[50];int data;
Node *Lchild; Node *Rchild;
Node(){ Lchild=NULL; Rchild=NULL;};
} *head=NULL;
int sorce[305],sort[305],n,pos;
char map[305][55],mapdata[305][55];
void Create(char *s,int t,Node * &node){
if(node){
if(t<=node->data)
Create(s,t,node->Lchild);
else Create(s,t,node->Rchild);
}
else {
node=new Node;
strcpy(node->name,s); node->data=t;
}
}
void print(Node * &x,bool flag){
if(x){
print(x->Rchild,flag);
if(flag) printf("%s %d\n",x->name,x->data);
else {
strcpy(map[pos],x->name);
sort[pos++]=x->data;
}
print(x->Lchild,flag);
}
else return ;
}
int main(){
while(~scanf("%d",&n)){
head=NULL;
pos=0;int m=n;
while(n--){
char name[55];int temp;
scanf("%s%d",name,&temp);
Create(name,temp,head);
}
for(int i=0;i<m;i++)
scanf("%s%d",mapdata[i],&sorce[i]);
print(head,false);
bool Is_sort=true; bool Is_stable=true;
for(int i=0;i<pos;i++){
if(sorce[i]!=sort[i]){
Is_sort=false;break;
}
else if(strcmp(map[i],mapdata[i])){
Is_stable=false;
}
}
if(!Is_sort){
printf("Error\n");print(head,true);
}
else if(!Is_stable){
printf("Not Stable\n");print(head,true);
}
else printf("Right\n");
}
return 0;
}
原文地址:http://blog.csdn.net/mummyding/article/details/38555779