标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 20301 Accepted Submission(s): 8855
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> using namespace std; struct tree { tree *child[26]; int n; }; void insert(char danci[],tree *root) { tree *ans=root; int l=strlen(danci); for(int i=0;i<l;i++) { if(ans->child[danci[i]-‘a‘]==NULL) { tree *temp; temp=(tree*)malloc(sizeof(tree)); temp->n=1; for(int j=0;j<26;j++) temp->child[j]=NULL; ans->child[danci[i]-‘a‘]=temp; ans=temp; } else { ans=ans->child[danci[i]-‘a‘]; ans->n++; } } } void search(char danci[],tree *root) { tree *ans=root; int l=strlen(danci); int i=0; for(;i<l;i++) { if(ans->child[danci[i]-‘a‘]!=NULL) { ans=ans->child[danci[i]-‘a‘]; } else break; } if(i==l) cout<<ans->n<<endl; else cout<<0<<endl; } int main() { tree *root=(tree *)malloc(sizeof(tree)); root->n=0; for(int i=0;i<26;i++) root->child[i]=NULL; char danci[12]; gets(danci); while(danci[0]!=‘\0‘) { insert(danci,root); gets(danci); } while(cin>>danci) { search(danci,root); } return 0; }
标签:
原文地址:http://www.cnblogs.com/lthb/p/4353261.html