Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这TRIE树怎么错了,大牛看看#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator