• 模板


    字典树Trie,还是数组的好,草。

    const int MAXN = 500000;
    
    struct TrieNode {
        TrieNode *ch[26];
    };
    
    struct Trie {
        TrieNode tn[MAXN + 5], *root;
        int top;
    
        inline void Init() {
            top = 0;
            root = NewNode();
        }
        inline TrieNode *NewNode() {
            for(int i = 0; i < 10; ++i)
                tn[top].ch[i] = nullptr;
            return &tn[top++];
        }
        inline TrieNode *Insert(char *s) {
            TrieNode *cur = root;
            for(char *i = s; *i != ''; ++i) {
                TrieNode *(&next)=cur->ch[*i - 'a'];
                if(!next)
                    next = NewNode();
                cur = next;
            }
            return cur;
        }
        inline TrieNode *Query(char *s) {
            TrieNode *cur = root;
            for(char *i = s; *i != ''; ++i) {
                TrieNode *(&next)=cur->ch[*i - 'a'];
                if(!next)
                    return nullptr;
                cur = next;
            }
            return cur;
        }
    } trie;
    
    const int MAXN = 5000000;
    
    struct TrieNode {
        int ch[26];
    };
    
    struct Trie {
        TrieNode tn[MAXN + 5], root=0;
        int top;
    
        inline void Init() {
            top = 0;
            root = NewNode();
        }
        inline int NewNode() {
            memset(tn[top].ch,0,sizeof(tn[top].ch));
            return top++;
        }
        inline int Insert(char *s) {
            int cur = root;
            for(char *i = s; *i != ''; ++i) {
                int t=*i - 'a';
                if(!tn[cur].ch[t])
                    tn[cur].ch[t] = NewNode();
                cur = tn[cur].ch[t];
            }
            return cur;
        }
        inline bool Query(char *s) {
            int cur = root;
            for(char *i = s; *i != ''; ++i) {
                int t=*i - 'a';
                if(!tn[cur].ch[t])
                    return false;
                cur = tn[cur].ch[t];
            }
            return true;
        }
    } trie;
    
  • 相关阅读:
    Javascript-DOM笔记
    JavaScript面向切面编程入门
    JavaScript面向对象编程入门
    AngularJS入门笔记
    Windows 安装 mysql-5.7.12-winx64(CommunityServer) 备忘
    PowerDesigner 16.5 链接SQL Server 2008R2
    C# GDI
    C# I/O
    Aspose.Words CookieBook
    EasyUI datagrid 多条件查询
  • 原文地址:https://www.cnblogs.com/Yinku/p/11247784.html
Copyright © 2020-2023  润新知