Dictionary Tree Array Template

Source: Internet
Author: User

Oneself is too lazy, do not have to write another code, this is another blog directly to find, its array template written very good, pointer template general like it! ~~

http://blog.csdn.net/king_cannon_fodder/article/details/77175620

Here is an example of the number of occurrences of the query string after the input string # include <bits/stdc++.h> #define MAXN 1000000using namespace std;struct trie{int next[2 6];///indicates whether the next node exists, the data field of the Int val;///node, can be changed as needed} Tree[maxn];int NXT;///NXT represents the number of the node char str[26];///stores the input string int add () {/    Establish a new node memset (&AMP;TREE[NXT], 0, sizeof (Trie)); return nxt++;}    void Insert (char *s)///insert and build {int rt = 0, Len = strlen (s);        for (int i = 0; i < len; i++) {int c = s[i]-' a ';        if (!tree[rt].next[c]) {///If the node does not already exist in the tree, open a new node tree[rt].next[c] = Add ();    } RT = Tree[rt].next[c];    The tree[rt].val++;///maintains the data field (this indicates the number of occurrences)}bool find (char *s) {///finds the string if there is an int rt = 0, Len = strlen (s);        for (int i = 0; i < len; i++) {int c = s[i]-' a ';        if (!tree[rt].next[c]) return false;    RT = Tree[rt].next[c];    } if (Tree[rt].val) return true; return false;}    int main () {int T;    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;}

An array template is actually a mock pointer! ! ~

Dictionary Tree Array Template

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.