Hihocoder 1014 Trie Tree (Trie)

Source: Internet
Author: User

#1014: Trie Tree
Time limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe

Small hi and small ho is a pair of good friends, born in the information society, they have a great interest in programming, they agreed to help each other, in the programming of learning along the road together.

This day, they met a dictionary, so little hi to small ho raised the classic question: "Little ho, can you for each of the strings I gave, in this dictionary to find all the words starting with this string?" "

The battle-hardened Little Ho replied: "How can not it!" You give me a string, I go through all the words in the dictionary, check that you give me the string is not the prefix of this word is it? "

Little hi smiled: "You ah, still too young!" ~ Suppose there are 100,000 words in this dictionary, I ask you 10,000 times, which month do you have to count? ”

Small Ho bowed down a count, look at that pile of 0, immediately feel oneself this life will spend on above ...

Small hi look at small ho's embarrassing kind, also continue to smile: "Let me improve your knowledge level bar ~ Do you know the tree such a data structure?" ”

Small Ho thought, said: "Know ~ it is a basic data structure, as here said!" ”

Small hi satisfied nodded, said: "Then you know how I use a tree to represent the whole dictionary?" ”

Little Ho shook his head and said he was not clear.

Hint one: establishment of trie tree

"You see, we have now got such a tree, then you see, if I give you a string AP, how do you find all the words that start with the AP?" "Little hi again began to test Xiao Ho."

"Hmm ... Iterate through all the words? Little Ho still does not forget the algorithm that he first proposed.

Stupid This tree is built in white! "Little Hi, after the little Ho," continued the way: "Watch! ”

Tip Two: How to use the trie tree

Tip Three: In the establishment of trie tree at the same time statistics!

"Then now!" Go to the code to achieve it! "said Little hi.

Input

The first behavior of the input is a positive integer n, which indicates the size of the dictionary, followed by n lines, one word per line (not guaranteed to be an English word, or possibly a Martian word), a word consisting of no more than 10 lowercase English letters, possibly with the same word, which should be treated as a different word. The next behavior is a positive integer m, which indicates the number of times the small hi inquired, followed by the M-line, each line a string consisting of a lowercase English letter of not more than 10, which represents a query for small hi.

In 20% of the data is N, m<=10, the dictionary size <=2.

In 60% of the data is N, m<=1000, the dictionary size <=5.

In 100% of the data is N, m<=100000, the dictionary size <=26.

The subject by the amount of data to be ranked Oh ~

Output

For each query of little hi, output an integer ans, which represents the number of words prefixed by the string in the dictionary given by small hi.

Sample input
5babaabbabbbaaaaabbaaaaaabaababaababb5babbbaabaaababbbbbabbaab
Sample output
10300


Parse: Trie tree template. When it is established, it is marked once for each node, so that it represents a string prefixed by the string preceded by this character. You can find it directly when you ask.




AC Code:

#include <bits/stdc++.h>using namespace Std;const int maxnode = 100000 * 10 + 5;    Maximum number of nodes = maximum number of words * maximum word length const int sigma_size = 26;struct trie{int ch[maxnode][sigma_size];    int Val[maxnode];    int sz;  void Clear () {sz = 1; memset (Ch[0], 0, sizeof (ch[0]));}    There was only one root node int idx (char c) {return C-' a ';}        void Insert (string s) {int u = 0, n = s.size ();            for (int i=0; i<n; i++) {int c = idx (s[i]);                if (!ch[u][c]) {memset (Ch[sz], 0, sizeof (Ch[sz]));                Val[sz] = 0;            CH[U][C] = sz++;            } Val[u] + +;        U = ch[u][c];    } Val[u] + +;        } int Find (string s) {int u = 0, n = s.size ();            for (int i=0; i<n; i++) {int c = idx (s[i]);            if (!ch[u][c]) return 0;        U = ch[u][c];    } return Val[u]; }};    Trie T;int Main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//SXK string s; INT N, m;        while (cin>>n) {t.clear ();           for (int i=0; i<n; i++) {cin>>s;        T.insert (s);        } cin>>m;            for (int i=0; i<m; i++) {cin>>s;        printf ("%d\n", T.find (s)); }} return 0;}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hihocoder 1014 Trie Tree (Trie)

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.