HDU 1251 统计难题

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct T

{

    T *ch[26];

    int n,flag;

}rt;

char in[15];

void init(T *t)

{

    for(int i=0;i<26;i++)

    {

       t->ch[i]=NULL;

    }

    t->n=t->flag=0;

}

void insert(T *t,char *in)

{

    if(*in)

    {

         if(t->ch[*in-'a']==NULL)

         {

             t->ch[*in-'a']=(T *)malloc(sizeof(T));

             init(t->ch[*in-'a']);

         }

         (t->ch[*in-'a']->n)++;

         insert(t->ch[*in-'a'],in+1);

    }

    else

    {

        t->flag=1;    

    }

}

int find(T *t,char *in)

{

    if(t->ch[*in-'a']!=NULL)

    {

         if(*(in+1)=='\0')

         {

             return t->ch[*in-'a']->n;

         }

         else

         {

             return find(t->ch[*in-'a'],in+1);

         }

    }

    else

    {

         return 0;

    }

}

int main()

{

    T *s;

    s=(T *)malloc(sizeof(T));

    init(s);

    while(gets(in))

    {

        if(strlen(in)==0)

           break;//空行代表单词表的结束,以此来控制。 

        insert(s,in);

    }

    while(scanf("%s",in)!=EOF)

    {

        printf("%d\n",find(s,in));

    }

    //system("pause");

    return 0;

}

你可能感兴趣的:(HDU)