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 fql5188 at 2008-07-18 17:53:05 on Problem 1002
In Reply To:做字典这类题的心得. Posted by:fql5188 at 2008-07-18 17:51:53
这也是我看别人的二叉查找树,很简单,很容易上手.
#include<stdio.h>
#include<string.h>
#include<ctype.h>	
int mark=0;//输出标记。
struct node
{
	char s[20];
	int count;
	node *lchild;
	node *rchild;
};
node *insertT(node *root,char s[])
{
	if(root==NULL)
	{
		node *p;
		p=new node;
		strcpy(p->s,s);
		p->count=1;
		p->lchild=NULL;
        p->rchild=NULL;
		return p;
	}
	else if(strcmp(s,root->s)<0)
	{
		root->lchild=insertT(root->lchild,s);
	}
	else if(strcmp(s,root->s)>0)
	{
		root->rchild=insertT(root->rchild,s);
	}
	else
		root->count+=1;
	return root;
}
void midorder(node *root)
{
	if(root!=NULL)
	{
		midorder(root->lchild);
		if(root->count>1)
		{
			printf("%s %d\n",root->s,root->count);
			mark=1;
		}
		midorder(root->rchild);
	}
}
int main()
{
	char march[2][25]={{"ABCDEFGHIJKLMNOPRSTUVWXY"},{"222333444555666777888999"}};
    int m;	
	node *root;
	root=NULL;
	scanf("%d",&m);
	while(m--)
	{
		char str[50];
		char s[20];
		scanf("%s",str);
		int i,j,k,len;
		len=strlen(str);
		k=0;
		for(i=0;i<len;i++)
		{    
		    if(k==3)
			{
                 s[k]='-';
				 k++;
				 i-=1;
				 continue;
			}
			if(str[i]=='-')
				continue;
		  if(isalpha(str[i]))
		  {
			 for(j=0;j<24;j++)
			{
			   if(str[i]==march[0][j])
				{
                  s[k]=march[1][j];
				  k++;
				  break;
				}
			}
		  }
          if(str[i]>='0'&&str[i]<='9')
		  {
			  s[k]=str[i];
			  k++;
		  }
		}
		s[k]='\0';
	    root=insertT(root,s);
	}
	midorder(root);
	if(mark==0)
		printf("No duplicates.\n");
   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