标签:方便 pre 输入 idt 题目 relative 包括 输出 memory
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1423 Accepted Submission(s): 426
#include<bits/stdc++.h> using namespace std; struct node { int have; node *child[26]; node(){have=0;for(int i=0;i<26;++i) child[i]=NULL;} }; node *root; void release(node *p) { if(p==NULL) return; for(int i=0;i<26;++i){ if(p->child[i]!=NULL) release(p->child[i]); } delete p; } void Insert(char *s) { node *p=root; int n1=strlen(s); for(int i=0;i<n1;++i){int t=s[i]-‘a‘; if(p->child[t]==NULL) p->child[t]=new node(); p=p->child[t]; p->have++; } } void Delete(char *s) { node *p=root,*pre=p; int n1=strlen(s),t,num=0; for(int i=0;i<n1;++i){ t=s[i]-‘a‘; if(p->child[t]==NULL) return; pre=p; p=p->child[t]; }num=p->have; release(p); pre->child[t]=NULL; p=root; for(int i=0;i<n1-1;++i){ p=p->child[s[i]-‘a‘]; p->have-=num; } } bool Search(char *s) { node *p=root; int n1=strlen(s); for(int i=0;i<n1;++i){ int t=s[i]-‘a‘; if(p->child[t]==NULL) return 0; p=p->child[t]; } if((p->have)<1) return 0; return 1; } int main() { int N,i,j; char s1[25],s2[35]; cin>>N; root=new node(); while(N--){ scanf("%s%s",s1,s2); if(!strcmp(s1,"insert")){ Insert(s2) ; } else if(!strcmp(s1,"delete")){ Delete(s2); } else if(!strcmp(s1,"search")){ Search(s2)?puts("Yes"):puts("No"); } }release(root); return 0; }
标签:方便 pre 输入 idt 题目 relative 包括 输出 memory
原文地址:http://www.cnblogs.com/zzqc/p/7204863.html