Time limit: 10000ms single point time limit: 1000ms memory 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
- Ah yo, long time no programming, hope to slowly pick it back.
-
reference to this buddy's: http://blog.csdn.net/hguisu/article/details/8131559
-
I didn't even use the basic struct and class pointers at first. Always overflow, miserable. After watching a kind of suddenly enlightened feeling, and then coding, write a run, RE, I looked for a while to find may be stack overflow, and then put Char w[100000][27] put on the outside of main, after the AC.
-
trie tree is the word search tree, the most important role is the space compression and "instantaneous" search, the algorithm idea is:
-
{
-
build a tree;
-
Each node of the tree has a maximum of 26 pointers (because a total of 26 an English letter);
-
each letter corresponds to a subscript word-' a ';
-
one layer down;
-
This dude taught me to mix class and struct. , struct can also have a constructor, is also drunk!
-
--just after the New Year's Day to come to the library to knock code, I also pretty spell, haha
-
#define_crt_secure_no_deprecate#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>using namespacestd;classtrie{ Public: Trie (); Trie (Trie&tr); ~Trie () {}voidInsertChar*word); intSearchChar*word);protected: structnode{Chardata; intfreq; Node*branch[ -];//BranchNode ();//constructor Function }; Node*root;//root node (pointer)}; Trie::node::node () {//data = NULL;Freq =0; memset (Branch,0,sizeof(branch));} Trie::trie (): root (NULL) {}voidTrie::insert (Char*word) { if(root = NULL) root =Newnode (); intLen =strlen (word); Node*p =Root; for(inti =0; i < Len; i++){ intK = Word[i]-'a'; if(P->branch[k] = =NULL) {P->BRANCH[K] =Newnode (); P->data =Word[i]; } //p->freq++;p->branch[k]->freq++; P= p->Branch[k]; }}intTrie::search (Char*word) { intLen =strlen (word); intCNT =0; Node*p =Root; for(inti =0; i < Len; i++){ intK = Word[i]-'a'; if(P->branch[k] = = NULL)return 0; //cout << p->data;CNT = p->branch[k]->freq; P= p->Branch[k]; } returnCNT;}Charw[100000][ -];intMain () {intN, M; Trie*t =NewTrie (); scanf ("%d", &N); for(inti =0; I < n; i++) {scanf ("%s", &w[1]); //printf (":%s%d\n", W, Strlen (w));T->insert (w[1]); } scanf ("%d", &m); for(inti =0; I < m; i++) {scanf ("%s", &W[i]); //printf (":%s%d\n", W, Strlen (w)); //cout << T->search (w) << Endl; } for(inti =0; I < m; i++){ //scanf ("%s", &w[i]); //printf (":%s%d\n", W, Strlen (w));cout << T->search (w[i]) <<Endl; } //delete[] T; //System ("pause"); return 0;}
#1014: Trie tree