hdoj-1251-tire树模板

来源:互联网 发布:中国食品安全问题数据 编辑:程序博客网 时间:2024/05/02 02:13
#include<cstdio>#include<cstring>using namespace std;struct tire{    int cnt;    tire *next[26];}; tire *root = new tire;void BuildTire(char *str){    tire *p = root;    int l = strlen(str);    for(int i=0;i<l;i++)    {        int a = str[i]-'a';        if(p->next[a]==NULL)        {            tire *q = new tire;            q->cnt = 0;            for(int j=0;j<26;j++)              q->next[j] = NULL;            p->next[a] = q;            q->cnt++;            p = q;           }        else{            p = p->next[a];            p->cnt++;        }    }}int FindTire(char *str){    int  l =strlen(str);    tire *p = root;    for(int i=0;i<l;i++)    {        int a = str[i]-'a';       if(p->next[a]==NULL)return 0;           p = p->next[a];    }    return p->cnt;      }int main(){    char str[100];    root->cnt = 0;    for(int i=0;i<26;i++)      root->next[i] = NULL;    while(gets(str)&&str[0]!='\0'){        BuildTire(str);    }    while(scanf("%s",str)!=EOF){       printf("%d\n",FindTire(str));        }    return 0;}
0 0