Time Limit: 4000/2000 MS
(Java/Others) Memory Limit: 131070/65535 K
(Java/Others)
Total Submission(s): 15844 Accepted
Submission(s): 6777
1 //140MS 43828K 1134 B G++ 2 #include<stdio.h> 3 #include<string.h> 4 struct node{ 5 int cnt; 6 node *next[27]; 7 node(){ 8 cnt=0; 9 memset(next,0,sizeof(next)); 10 } 11 }; 12 void insert(node *root,char s[]) 13 { 14 int i=0; 15 node *p=root; 16 while(s[i]){ 17 int j=s[i++]-‘a‘; 18 if(p->next[j]==NULL) p->next[j]=new node(); 19 p=p->next[j]; 20 p->cnt+=1; 21 //printf("**%d\n",p->cnt); 22 } 23 } 24 int judge(node *root,char s[]) 25 { 26 int i=0; 27 node *p=root; 28 int flag=0; 29 while(s[i]){ 30 int j=s[i++]-‘a‘; 31 if(p->next[j]==NULL){ 32 flag=0;break; 33 } 34 p=p->next[j]; 35 flag=p->cnt; 36 } 37 return flag; 38 } 39 int main(void) 40 { 41 node *root=new node(); 42 char word[10]; 43 char cur[10]; 44 //freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); 45 //freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout); 46 while(gets(word)){ 47 if(strcmp(word,"")==0) break; 48 insert(root,word); 49 } 50 while(gets(cur)){ 51 printf("%d\n",judge(root,cur)); 52 } 53 return 0; 54 }
hdu 1251 统计难题 (字典树),布布扣,bubuko.com
原文:http://www.cnblogs.com/GO-NO-1/p/3622875.html