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-17 17:58:25 on Problem 2503 and last updated at 2009-07-17 18:23:09
#include<stdio.h> 
#include<string.h>
const int kind=26;
#define MAX 30
struct Treenode
{ 
    char s[MAX];
    Treenode *next[kind];
    Treenode()
    { 
        for(int i=0;i<kind;i++) 
           next[i]=NULL; 
    } 
}; 

void insert(Treenode *&root,char *word,char *str)
{ 
    Treenode *location=root; 
    int i=0,branch=0; 
    if(location==NULL)
	{
		location=new Treenode();
		root=location;
	} 
    while(word[i]) 
    { 
        branch=word[i]-'a'; 
		if(location->next[branch]) 
			;
        else
			location->next[branch]=new Treenode();
        i++; 
        location=location->next[branch]; 
		strcpy(location->s,"eh");
    } 
	strcpy(location->s,str);
} 
void search(Treenode *root,char *word)
{ 
    Treenode *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))
      printf("%s\n",location->s);
	else
		printf("eh\n");
} 
int main() 
{ 
    char word[MAX],str[MAX]; 
    char ask[MAX]; 
    Treenode *root=NULL; 
    while(scanf("%s %s",str,word)!=EOF) 
    { 
	//	cout<<str<<" "<<word<<endl;
        if(word[0]=='\0') break; 
        insert(root,word,str); 
    } 
	//cout<<"start"<<endl;
    while(gets(ask)) 
	{
		search(root,ask);
	}
    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