codevs 1729 单词查找树

二次联通门 : codevs 1729 单词查找树

/*
    codevs 1729 单词查找树
     
     
    Trie树
    
    统计节点个数
    建一棵Trie树
    插入单词时每新开一个节点就计数器加1
     
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>

void read (int &now)
{
    now = 0;
    register char word = getchar ();
    while (word < '0' || word > '9')
        word = getchar ();
    while (word >= '0' && word <= '9')
    {
        now = now * 10 + word - '0';
        word = getchar ();
    }
}

struct Trie_Type
{

    struct Trie
    {
        Trie *next[26];
        bool Add_flag;
        bool flag;
    };
        
    Trie Root;
    
    int Count;
     
    void Insert (char *line)
    {
        int Length = strlen (line);
        Trie *now = &Root;
        Trie *res;
        for (int i = 0; i < Length; i++)
        {
            int name = line[i] - 'A';
            if (now->next[name] == NULL)
            {
                res = (Trie *) malloc (sizeof Root);
                res->flag = true;
                for (int j = 0; j < 26; j++)
                    res->next[j] = NULL;
                now->next[name] = res;
                Count++;
                now = now->next[name];
            }
            else
            {
                now->next[name]->flag = true;
                now = now->next[name];
            }
        }
    }
    
    void Clear ()
    {
        for (int i = 0; i < 26; i++)
            Root.next[i] = NULL;
    }
    
};

Trie_Type Make;
int N;

int main (int argc, char *argv[])
{
    Make.Clear ();
    register char word[65];
    while (scanf ("%s", word) != EOF)
        Make.Insert (word); 
    printf ("%d", Make.Count + 1);
    return 0;
}
原文地址:https://www.cnblogs.com/ZlycerQan/p/6753759.html