zoukankan      html  css  js  c++  java
  • codevs 4189 字典

    二次联通门 : codevs 4189 字典

    /*
        codevs 4189 字典 
        
        Trie树
        
        裸题
        用指针写的..
         
    */
    #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 End;
            bool flag;
        };
            
        Trie Root;
        
        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;
                    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;
        }
        
        bool Search (char *line)
        {
            int Length = strlen (line);
            Trie *now = &Root;
            int name;
            for (int i = 0; i < Length; i++)
            {
                name = line[i] - 'a';
                now = now->next[name];
                if (now == NULL)
                    return false;
            }    
            return true;
        }
    };
    
    Trie_Type Make;
    int N;
    
    int main (int argc, char *argv[])
    {
        read (N);
        Make.Clear ();
        register char word[10];
        for (int i = 1; i <= N; i++)
        {
            scanf ("%s", word);
            Make.Insert (word); 
        
        }
        read (N);
        while (N--)
        {
            scanf ("%s", word);
            printf (Make.Search (word) ? "YES" : "NO");
            putchar ('
    ');
        }
        return 0;
    }
  • 相关阅读:
    你应该掌握的——树和二叉树
    nyist oj 63(二叉树)
    非递归遍历二叉树的四种策略先序、中序、后序和层序
    学习的四种境界
    nyist oj 467 (中缀式变后缀式)
    二叉平衡树
    nyist OJ 35 (表达式求值)
    线索二叉树
    二叉树的三种遍历方法(递归和非递归)
    算法学习之路
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/6753645.html
Copyright © 2011-2022 走看看