Hdu 1251 统计难题

此题需用C++提交,不能用G++;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
    int sum;
    node *next[26];
}*tree,t;
tree root;
void Insert(char *str)
{
    tree p=root,newnode;
    for(; *str!=''; str++)
    {
        if(p->next[*str-'a']!=NULL)
        {
            p=p->next[*str-'a'];
            p->sum++;
        }
        else
        {
            newnode = new node();
            for(int i=0; i<26; i++)
                newnode->next[i]=NULL;
            newnode->sum=1;
            p->next[*str-'a']=newnode;
            p=newnode;
        }
    }
}
int Find(char *str)
{
    tree p=root;
    for(; *str!=''; str++)
    {
        if(p->next[*str-'a']!=NULL)
            p=p->next[*str-'a'];
        else
            return 0;
    }
    return p->sum;
}
int main()
{
    char str[15];
    root = new node();
    for(int i=0; i<26; i++)
        root->next[i]=NULL;
    root->sum=0;
    while(gets(str),strcmp(str,"")!=0)
        Insert(str);
    while(gets(str))
    {
        if(str[0]<'a'||str[0]>'z')
            break;
        printf("%d
",Find(str));
    }

    return 0;
}
原文地址:https://www.cnblogs.com/cn-blog-cn/p/4698619.html