Hihocoder #1014 trie Tree

Source: Internet
Author: User

#1014: Trie tree time limit:10000mscase time Limit:1000msmemory LIMIT:256MB Description

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

Problem solving: the dictionary tree
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <stack> - #defineLL Long Long - #definePII pair<int,int> the #defineINF 0x3f3f3f3f - using namespacestd; - Const intMAXN =1000000; - structtrie{ +     intwd[ -],cnt; -     voidinit () { +memset (WD,0,sizeof(wd)); ACNT =0; at     } - }; - trie DIC[MAXN]; - inttot,n,m; - voidINSERTWD (Char*s,introot) { -      for(inti =0; S[i]; ++i) { in         intK = S[i]-'a'; -         if(!Dic[root].wd[k]) { to dic[tot].init (); +Dic[root].wd[k] = tot++; -         } theRoot =Dic[root].wd[k]; *dic[root].cnt++; $     }Panax Notoginseng } - intQueryChar*s,introot) { the      for(inti =0; S[i]; ++i) { +         intK = S[i]-'a'; A         if(Dic[root].wd[k] = =0)return 0; theRoot =Dic[root].wd[k]; +     } -     returndic[root].cnt; $ } $ intMain () { -     Charstr[ -]; -      while(~SCANF ("%d",&N)) { the         intRoot =0; -tot =1;Wuyidic[0].init (); the          for(inti =0; I < n; ++i) { -scanf"%s", str); Wu INSERTWD (str,root); -         } Aboutscanf"%d",&m); $          for(inti =0; I < m; ++i) { -scanf"%s", str); -printf"%d\n", Query (Str,root)); -         } A     } +     return 0; the}
View Code

Hihocoder #1014 trie Tree

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.