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<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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator