After learning the dictionary tree for a while, I personally think it is easier to master the dictionary tree. However, ACM has a variety of questions. We only have to practice more to have a deeper understanding of the application of the dictionary tree.
The following describes the dictionary tree.
To master the dictionary tree, you only need to write a program about the dictionary tree and remember its structure. First, let's look at the definition of the dictionary tree.
[Cpp]
Struct Trie
{
Trie * next [MAX];
Bool isword;
};
Struct Trie
{
Trie * next [MAX];
Bool isword;
};
In fact, the definition of the above dictionary Tree node is only from a question I have made. The elements in it are determined by your questions. However, Trie * next [] is a certain array, however, his size depends on your question. (It may be 26 lower-case letters, 52 lower-case letters, or others)
I believe that everyone has mastered this example. I suggest you take A look at the question first. (After you have completed this question, you can certainly drop many other questions about the dictionary tree)
The general meaning of a question is that you need to find some words for a given word, which is composed of the other two words.
In fact, we use the ascii code of the character to give the corresponding index to it.
This question is to split each word into two parts to see if each part is a word in the dictionary tree.
Let's take a look at the process of building. (Be patient and understand the program step by step, just one time)
Draw a picture if necessary
[Cpp]
<SPAN style = "FONT-SIZE: 18px"> void createTrie (char str []) // input string
{
Int len = strlen (str );
Trie * p = root, * q = NULL;
For (int I = 0; I <len; I ++)
{
Int id = str [I]-'A ';
If (p-> next [id] = NULL)
{
Q = new Trie;
For (int j = 0; j <MAX; j ++)
Q-> next [j] = NULL;
Q-> isword = false;
P-> next [id] = q;
}
If (I = len-1)
P-> next [id]-> isword = true;
P = p-> next [id];
}
} </SPAN>
Void createTrie (char str []) // input string
{
Int len = strlen (str );
Trie * p = root, * q = NULL;
For (int I = 0; I <len; I ++)
{
Int id = str [I]-'A ';
If (p-> next [id] = NULL)
{
Q = new Trie;
For (int j = 0; j <MAX; j ++)
Q-> next [j] = NULL;
Q-> isword = false;
P-> next [id] = q;
}
If (I = len-1)
P-> next [id]-> isword = true;
P = p-> next [id];
}
} Let's look at the search process.
[Cpp]
<SPAN style = "FONT-SIZE: 18px"> bool findTrie (char str [])
{
Int len = strlen (str );
Trie * p = root;
For (int I = 0; I <len; I ++)
{
Int id = str [I]-'A ';
If (p-> next [id] = NULL)
{
Return false;
}
P = p-> next [id];
}
If (p-> isword)
Return true;
Else
Return false;
} </SPAN>
Bool findTrie (char str [])
{
Int len = strlen (str );
Trie * p = root;
For (int I = 0; I <len; I ++)
{
Int id = str [I]-'A ';
If (p-> next [id] = NULL)
{
Return false;
}
P = p-> next [id];
}
If (p-> isword)
Return true;
Else
Return false;
}
Finally, remember to release this dictionary tree.
[Cpp]
<SPAN style = "FONT-SIZE: 18px"> void del (Trie * root)
{
For (int I = 0; I <26; I ++)
{
If (root-> next [I]! = NULL)
{
Del (root-> next [I]);
}
}
Delete root;
} </SPAN>
Void del (Trie * root)
{
For (int I = 0; I <26; I ++)
{
If (root-> next [I]! = NULL)
{
Del (root-> next [I]);
}
}
Delete root;
} Note that when creating a tree, a root node Trie * root = new Trie is generated first; // I almost forgot to create a new one every time.
Paste the complete code below
[Cpp]
<SPAN style = "FONT-SIZE: 18px"> # include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace std;
Const int MAX = 26;
Struct Trie
{
Trie * next [MAX];
Bool isword;
};
Trie * root = new Trie;
Char word [50000] [30];
Void createTrie (char str [])
{
Int len = strlen (str );
Trie * p = root, * q = NULL;
For (int I = 0; I <len; I ++)
{
Int id = str [I]-'A ';
If (p-> next [id] = NULL)
{
Q = new Trie;
For (int j = 0; j <MAX; j ++)
Q-> next [j] = NULL;
Q-> isword = false;
P-> next [id] = q;
}
If (I = len-1)
P-> next [id]-> isword = true;
P = p-> next [id];
}
}
Bool findTrie (char str [])
{
Int len = strlen (str );
Trie * p = root;
For (int I = 0; I <len; I ++)
{
Int id = str [I]-'A ';
If (p-> next [id] = NULL)
{
Return false;
}
P = p-> next [id];
}
If (p-> isword)
Return true;
Else
Return false;
}
Void del (Trie * root)
{
For (int I = 0; I <MAX; I ++)
{
If (root-> next [I]! = NULL)
{
Del (root-> next [I]);
}
}
Delete root;
}
Int main ()
{
Int num = 0;
Char str1 [30], str2 [30];
For (int I = 0; I <MAX; I ++)
{
Root-> next [I] = NULL;
}
Root-> isword = false;
While (gets (word [num])
{
CreateTrie (word [num]);
Num ++;
}
For (int I = 0; I <num; I ++)
{
Int len = strlen (word [I]);
If (len = 1)
Continue;
For (int j = 0; j <len; j ++) // separate each part of a word.
{
Int k;
If (j = len-1) continue;
For (k = 0; k <= j; k ++)
{
Str1 [k] = word [I] [k];
}
Str1 [k] = '\ 0 ';
Int k2 = 0;
For (int l = k; l <len; l ++)
{
Str2 [k2 ++] = word [I] [l];
}
Str2 [k2] = '\ 0 ';
If (findTrie (str1) & findTrie (str2 ))
{
Cout <word [I] <endl;
Break; // The error is returned here (it may be repeated)
}
}
}
Del (root );
Return 0;
}
</SPAN>