hdu_1251统计难题

来源:互联网 发布:雅思作文 知乎 编辑:程序博客网 时间:2024/05/20 06:50
#include<cstdio>#include<cstring>#include<malloc.h>typedef struct node{    int count;    struct node *next[26];}*tree;void insert(tree h,char *s){    tree p=h;    int len=strlen(s),i;    for(i=0;i<len;i++)    {        int index=s[i]-'a';        if(p->next[index]!=NULL)        {            p=p->next[index];            p->count++;        }        else        {            tree tem=(tree)calloc(1,sizeof(node));            tem->count=1;            p->next[index]=tem;            p=tem;        }    }}int find(tree h,char *s){    tree p=h;    int len=strlen(s),i;    for(i=0;i<len;i++)    {        int index=s[i]-'a';        if(p->next[index]!=NULL)            p=p->next[index];        else return 0;    }    return p->count;}void delet(tree head){    int i;    for(i=0;i<26;i++)    {        if(head->next[i]!=NULL)            delet(head->next[i]);    }    free(head);}int main(){    tree head=(tree)calloc(1,sizeof(node));    char s[11];    while(gets(s)&&s[0])//注意着要用gets,来读空行    {        insert(head,s);    }    while(scanf("%s",s)!=EOF)    {        printf("%d\n",find(head,s));    }    delet(head);    return 0;}


 

0 0
原创粉丝点击