统计难题(hOJ 1251)
来源:互联网 发布:手机淘宝聊天记录恢复 编辑:程序博客网 时间:2024/06/08 11:22
#include<stdio.h>#include<string.h>#include<stdlib.h>struct distree{ struct distree *child[26]; int n;};struct distree * root;void insert(char str[]);void find(char str[]);main(){ char str[10]; int i; char findch[10]; root=(struct distree *)malloc(sizeof(struct distree )); for(i=0;i<26;i++) root->child[i]=0; while(gets(str),strcmp(str,"")!=0) { insert(str); } while(gets(findch)) { find(findch); } free(root);}void insert(char str[]){ int len; int i,j; struct distree * cout; struct distree * newnode; len=strlen(str); if(len==0) return; cout=root; for(i=0;i<len;i++) { if(cout->child[str[i]-'a']!=0) { cout=cout->child[str[i]-'a']; cout->n=cout->n+1; } else { newnode=(struct distree *)malloc(sizeof(struct distree)); for(j=0;j<26;j++) newnode->child[j]=0; cout->child[str[i]-'a']=newnode; cout=newnode; cout->n=1; } } }void find(char str[]){ int len,i; struct distree * cout; len=strlen(str); if(len==0) { printf("0\n"); return ; } cout=root; for(i=0;i<len;i++) { if(cout->child[str[i]-'a']!=0) { cout=cout->child[str[i]-'a']; } else { printf("0\n"); return ; } } printf("%d\n",cout->n);}