统计难题 HDU - 1251

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char s[15];
struct node
{
    int cnt,tag;
    node*son[26];
}*root;
node*build()
{
    node*tmp=new(node);
    tmp->cnt=0;
    memset(tmp->son,0,sizeof(tmp->son));
    return tmp;
}
void myinsert()
{
    char*p=s;
    node*r=root;
    while(*p)
    {
        int id=*p-'a';
        if(!r->son[id])
            r->son[id]=build();
        r=r->son[id];
        r->cnt++;
        p++;
    }
}
int query2()
{
    char*p=s;
    node*r=root;
    while(*p)
    {
        int id=*p-'a';
        if(!r->son[id])
            return 0;
        r=r->son[id];
        p++;
    }
    return r->cnt;
}
int main()
{
    root=build();
    while(1)
    {
        gets(s);
        if(!s[0]) break;
        myinsert();
    }
    while(scanf("%s",s)!=EOF)
    {
        printf("%d\n",query2());
    }
    return 0;
}

 

posted @ 2018-09-22 13:43  eason99  阅读(63)  评论(0编辑  收藏  举报