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;
    }
  • 相关阅读:
    P2764 最小路径覆盖问题
    P1402 酒店之王 网络流
    P2597 [ZJOI2012]灾难 拓扑排序
    FJOI2017 矩阵填数
    2019.2.27模拟
    2019.2.26模拟
    SDOI2013 方程
    [AH2017/HNOI2017]抛硬币
    Lucas定理和扩展Lucas定理
    LuoguP4861 按钮
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/6753645.html
Copyright © 2011-2022 走看看