Count the maximum number of times a word appears (Trie tree), trie tree
A Time Limit: 60 ms Memory limit: 65536 K in doubt? Click Here ^_^ The topic description contains n (1 <= n & n <= 2*10 ^ 6) strings, each of which only contains lowercase English letters and a maximum of five. Ask the maximum number of occurrences of the n strings. Enter a single group of inputs. Enter a number n in the first line, and then n rows. Each row contains a string. Output a number to indicate the answer. Sample Input
5abaabbwabaz
Sample output
2
See self-algorithm competition training guide. In other words, I love card STL.
# Include <iostream> # include <vector> # include <algorithm> # include <cstring> # include <cstdio> using namespace std; const int maxn = 900000; int ch [maxn] [26]; // ch [I] [j] The subnode that saves node I, whose ID is j, int val [maxn]; // record the Additional Information int sz, Max =-1; // sz indicates the total number of nodes void insert (char * s) // insert {int u = 0, len = strlen (s); for (int I = 0; I <len; I ++) {int c = s [I]-'A'; if (! Ch [u] [c]) {memset (ch [sz], 0, sizeof (ch [sz]); ch [u] [c] = sz ++ ;} u = ch [u] [c];} val [u] ++; if (Max <val [u]) Max = val [u];} int main () {char s [7]; int n; scanf ("% d", & n); getchar (); sz = 1; memset (ch [0], 0, sizeof (ch [0]); // At first, there is only one root node memset (val, 0, sizeof (val); while (n --) {scanf ("% s", s); insert (s);} printf ("% d \ n", Max); return 0 ;}
How to use Java to count the number of occurrences of a specific word?
The dictionary tree, also known as the word search tree and Trie tree, is a tree structure and a variant of the hash tree. A typical application is used for statistics, sorting, and saving a large number of strings (but not limited to strings). Therefore, it is often used by the search engine system for text word frequency statistics. It saves storage space by using the public prefix of strings and minimizes unnecessary string comparisons. The query efficiency is higher than that of hash tables.
Therefore, we recommend that you use the dictionary tree to do a lot of online knowledge.
Pascal implementation of the Trie word search tree
The Trie tree is the character tree, and its core idea is space for time. I used a lot of data on the Internet about this dictionary tree.
We will give you 100000 words of no more than 10 length. For each word, we need to judge whether it has appeared. If so, the first few positions appear.
Hash can be used for this question, but I want to introduce the trie tree. In some ways, it is more useful. For example, for a word, I want to ask if its prefix has appeared. In this way, hash is not feasible, but trie is very simple.
Now back to the example, if we use the most silly method, we need to find out whether there is a word in front of it. The complexity of this algorithm is O (n ^ 2 ). It is obviously unacceptable for the range of 100000. Now let's change our mind. If the word I want to query is abcd, I obviously don't have to consider the words that start with B, c, d, and f. However, you only need to check whether there is abcd in the name starting with. Similarly, in words starting with a, we only need to consider B as the second letter ...... This tree model is becoming clearer ......
Suppose there are six words, B, abc, abd, bcd, abcd, efg, and hii. The tree we build is like this.
For each node, the process from the root traversal to the node is a word. If the node is marked as red, it indicates that the word exists; otherwise, it does not exist.
Then, for a word, I only need to follow him to the corresponding node and check whether the node is marked as red to see if it has appeared. Marking this node in red is equivalent to inserting this word.
In this way, the query and insertion can be completed together, and the time used is only the word length. In this example, 10 is used.
We can see that the number of nodes in each layer of the trie tree is 26 ^ I. To save space. We use a dynamic linked list or an array to simulate the dynamics. The cost of space cannot exceed the number of words × word length.
The program is very easy to implement, just a few lines in the partition, so I will not write it. Let's think about it.
Leave a message if you still do not understand it.
The following provides a criterion for finding whether a word is in a given dictionary:
Program trie;
Type
Rec = record
Got: boolean;
Next: array ['A'... 'Z'] of Longint;
End;
Var
N, I, j, Now, Tn: Longint;
S: string;
T: array [1 .. 1000] of rec;
Flag: boolean;
Begin
Readln (n );
Tn: = 1;
T [1]. Got: = False;
Fillchar (T [1]. next, sizeof (T [1]. next), 0 );
For I: = 1 to n do
Begin
Readln (s );
Now: = 1;
For j: = 1 to length (s) do
If T [now]. Next [s [j] <> 0 then now: = t [now]. next [s [j] else
Begin
Inc (Tn );
T [tn]. Got: = false;
Fillchar (T [tn]. next, sizeof (T [tn]. next), 0 );
T [Now]. next [s [j]: = Tn;
Now: = Tn;
End;
T [now]. Got: = true;
End;
Readln (... the remaining full text>