"Trie" Trie dictionary tree template static pointer pool, array notation

Source: Internet
Author: User

Here is the array notation:

#include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream > #include <algorithm>using namespace std; #define IDX (x) x-' a '; const int MAXN = 1e6;struct Trie {int next[26    ]; int Val;}    Tree[maxn];int NXT, T;char str[maxn];int Add () {memset (&tree[nxt], 0, sizeof (Trie)); return nxt++;}    void Insert (char *s) {int rt = 0, Len = strlen (s);        for (int i = 0; i < len; i++) {int c = idx (s[i]);        if (!tree[rt].next[c]) {Tree[rt].next[c] = Add ();    } RT = Tree[rt].next[c]; } tree[rt].val++;}    BOOL Find (char *s) {int rt = 0, Len = strlen (s);        for (int i = 0; i < len; i++) {int c = idx (s[i]);        if (!tree[rt].next[c]) return false;    RT = Tree[rt].next[c];    } if (Tree[rt].val) return true; return false;}    int main () {memset (&tree[0], 0, sizeof (Trie));    NXT = 1;    scanf ("%d", &t);       while (t--) {scanf ("%s", str); Insert (str);        } while (~SCANF ("%s", str)) {if (Find (str)) puts ("exist");    Else puts ("none"); } return 0;}

here is the static pointer pool notation:

#include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream > #include <algorithm>using namespace std; #define IDX (x) x-' a '; const int MAXN = 1e6;struct Trie {Trie *next[26]; int Val;} Tree[maxn];int NXT, T;char STR[MAXN];    Trie *add () {memset (&tree[nxt], 0, sizeof (Trie)); return &tree[nxt++];}    void Insert (Trie *rt, char *s) {int len = strlen (s);        for (int i = 0; i < len; i++) {int c = idx (s[i]);        if (!rt->next[c]) rt->next[c] = Add ();    RT = rt->next[c]; } rt->val++;}    BOOL Find (Trie *rt, char *s) {int len = strlen (s);        for (int i = 0; i < len; i++) {int c = idx (s[i]);        if (!rt->next[c]) return false;    RT = rt->next[c];    } if (Rt->val) return true; return false;}    int main () {NXT = 0;    Trie *root = Add ();    scanf ("%d", &t);        while (t--) {scanf ("%s", str);    Insert (root, str); } while (~SCANF ("%s", str)) {if (Find (root, str)) puts ("exist");    Else puts ("none"); } return 0;}


"Trie" Trie dictionary tree template static pointer pool, array notation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.