Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

哪位大大看看,RE?

Posted by flyingfish at 2004-09-09 15:56:46 on Problem 1760
#include<stdio.h>
#include<string.h>
typedef struct tree tree;
struct tree
{
	char name[10];
	tree *brother,*child;
};
tree *head=NULL;
char input[100];
void insert(tree* &sym,int pos)
{
	int i,j;
	char name[10];
	tree *nod,*tmp,*pre1,*pre2;
	for(i=pos,j=0;i<(int)strlen(input);i++,j++)
	{
		if(input[i]=='\\')
		{
			i++;
			break;
		}
		name[j]=input[i];
	}
	name[j]='\0';
	if(sym==NULL)
	{
		nod=new(tree);
		strcpy(nod->name,name);
		nod->brother=NULL;
		nod->child=NULL;
		sym=nod;
	}
	else if(strcmp(name,sym->name)<0)
	{
		nod=new(tree);
		strcpy(nod->name,name);
		nod->child=NULL;
		nod->brother=sym;
		sym=nod;
	}
	else if(strcmp(name,sym->name)==0)
		nod=sym;
	else 
	{
		pre2=NULL;
		pre1=sym;tmp=sym->brother;
		while(tmp!=NULL&&strcmp(name,tmp->name)>0)
		{
			pre2=pre1;
			pre1=tmp;
			tmp=tmp->brother;
		}
		if(tmp!=NULL&&strcmp(name,tmp->name)==0)
			nod=tmp;
		else if(tmp==NULL)
		{
			nod=new(tree);
			strcpy(nod->name,name);
			nod->child=NULL;
			nod->brother=pre1;
			pre2->brother=nod;
		}
		else 
		{
			nod=new(tree);
			strcpy(nod->name,name);
			nod->child=NULL;
			nod->brother=tmp;
			pre1->brother=nod;
		}
	}
	if(i<(int)strlen(input))
		insert(nod->child,i);
}
void ouput(tree *sym,int num)
{
	int i; 
	tree *tmp,*temp;   
	tmp=sym; 
	while(tmp!=NULL)
	{  
		for(i=0;i<num;i++) 
			printf(" ");
		printf("%s\n",tmp->name); 
		if(tmp->child!=NULL)
			ouput(tmp->child,num+1); 
		temp=tmp;
		tmp=tmp->brother; 
		delete temp;
	} 
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",input);
		insert(head,0);
	}
	ouput(head,0);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator