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

这TRIE树怎么错了,大牛看看

Posted by dynamic_study at 2009-07-29 17:56:32 on Problem 2503
#include<iostream>
using namespace std;
const int kind =26;
#define MAX 12
struct tree
{
	int i;
	char s[MAX];
	tree *next[kind];
	tree()
	{
		for(i=0;i<kind;i++)
			next[i]=NULL;
		strcpy(s,"eh");
	}
};

void insert(tree *&root ,char word[],char str[])
{
	tree *L=root;
	int i=0,son=0;
	if(L==NULL)
	{
		L=new tree();
		root=L;
	}
	while(word[i])
	{
		son=word[i]-'a';
		if(L->next[son]==NULL)
			L->next[son]=new tree();
		i++;
		L=L->next[son];

	}
	strcpy(L->s,str);
	//cout<<L->s<<endl;
}
void search(tree *root,char word[])
{ 
    tree *location=root; 
    int i=0,branch=0,len,flag=0; 
	len=strlen(word);
//	cout<<len<<endl;
    while(word[i]) 
    { 
        branch=word[i]-'a'; 
        if(!location->next[branch])
		{
			flag=1; 
			break;
		}
        i++; 
        location=location->next[branch]; 
    
    } 
	if((flag==0)||(i>=len))
      cout<<location->s<<endl;
	else
		cout<<"eh"<<endl;
} 
int main()
{
	char word[15],str[15],ser[15];
	tree *root=NULL;
	while(cin>>str)
	{   
		if(strcmp(str,"")==0) 
	      break;
		cin>>word;
		insert(root,word,str);
	}
	//cout<<"start"<<endl;
	while(gets(ser))
	{
		search(root,ser);
	}
     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