统计难题 HDU

#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
",query2());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/eason9906/p/11754870.html