hdu 1251 统计难题

来源:互联网 发布:c语言摄氏和华氏的换算 编辑:程序博客网 时间:2024/04/29 01:23

/*

第一个字典树。。虽然很早就理解了原理,但都没做过,这题水,直接模板了

初学的可以看看

*/

#include <stdio.h>#include <string.h>#define MAX 27struct node{    int num;    struct node *next[MAX]; }*q;void build(char *s,node *p){    int i,j;    for(i=0;i<strlen(s);i++)    {        if(p->next[s[i]-'a']==NULL)        {            q=new node;            for(j=0;j<26;j++)            q->next[j]=NULL;            p->next[s[i]-'a']=q;            p=p->next[s[i]-'a'];            p->num=1;        }        else        {            p=p->next[s[i]-'a'];            p->num++;        }    }}int find(char *s,node *p){    for(int i=0;i<strlen(s);i++)    {        if(p->next[s[i]-'a']==NULL)        return 0;        p=p->next[s[i]-'a'];    }    return p->num;}int main(){    char s[11];    node *root=new node;    for(int i=0;i<26;i++)    root->next[i]=NULL;    while(gets(s)&&s[0]!='\0')    build(s,root);    while(gets(s))    printf("%d\n",find(s,root));    return 0;}


原创粉丝点击