Question: http://acm.jlu.edu.cn/joj/showproblem.php? Pid = 1, 1058
Algorithm: Trie tree, breadth-first search
Idea: Build a dictionary into a Trie tree. Each time a key sequence is processed, a queue is established and processed cyclically. During the initial cycle, the Trie root node is queued. During each cycle, traverse all the child nodes of each node in the queue, and queue all the nodes of the child nodes that are the same as the keys currently processed, select the node with the highest priority in the current queue, output the path from the root node to the node, and then start a new cycle to process the next button in the Key sequence.
This question can also be done in another way, that is, when the dictionary is read, the key sequence of the current word is generated at the same time. After reading the key sequence, search in the dictionary sequentially. During the search process, the priority of words with the same prefix must be accumulated. It is easier to use the Trie tree.
The code for using the Trie tree is as follows:
- /* {JOJ1058: T9}
- * Date & Time: Thu Oct 09 19:42:13 2008
- * File: 1058.c
- * Author: WangHaibin
- * Platform: windows xp sp2, emacs 22.1, gcc, gdb
- *
- * Algorithm: Trie tree, wide search
- *
- * State: Acceptd, 0.00 s, 416 K
- **/
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- Typedef struct _ Tn {
- Char letter;
- Int frequency;
- Char belongto;
- Struct _ Tn * child, * brother, * parent;
- Char iscomplete;
- } Trienode;
- Trienode * root;
- Char belongto [26] = {'2', '2', '2', '3', '3', '4', '4 ', '4', '5', '5', '5', '6', '6', '6 ',
- '7', '7', '7', '7', '8', '8', '8', '9', '9', '9 ', '9 '};
- Void insert (const char * word, int freq ){
- Trienode * P = root, * oldbrother = NULL, * DEST = root-> child;
- Int K = 0, Len = strlen (Word );
- For (k = 0; k <Len; k ++ ){
- While (DEST! = NULL & DEST-> letter! = Word [k]) {
- Oldbrother = DEST, DEST = DEST-> brother;
- }
- If (DEST ){
- DeST-> Frequency + = freq;
- } Else {
- /* DEST is null, word [0] Not found */
- DeST = (trienode *) malloc (sizeof (trienode ));
- DeST-> letter = word [k];
- DeST-> frequency = freq;
- DeST-> parent = P;
- DeST-> brother = NULL;
- DeST-> child = NULL;
- DeST-> belongto = belongto [word [k]-'a'];
- DeST-> iscomplete = 0;
- If (oldbrother ){
- Oldbrother-> brother = DEST;
- } Else {
- /* If the value of oldbrother is null, the subtree of P is empty */
- P-> child = DEST;
- }
- }
- P = DEST;
- DeST = p-> child;
- Oldbrother = NULL;
- }
- P-> iscomplete = 1;
- }
- Void solve (const char * SEQ ){
- Trienode * queue [1024];
- Int front = 0, rear = 0, K = 0, I, j, maxfreq;
- Trienode * curlayer;
- Char path [128] = {0 };
- Queue [rear ++] = root;
- While (SEQ [k]! = '1 '){
- I = rear-front;
- For (j = 1; j <= I; j ++ ){
- Curlayer = queue [Front]-> child;
- Front ++;
- While (curlayer ){
- If (curlayer-> belongto = seq [k]) {
- Queue [rear ++] = curlayer;
- }
- Curlayer = curlayer-> brother;
- }
- }
- If (front = rear ){
- /* Queue is empty */
- Printf ("manually/N ");
- K ++;
- Continue;
- }
- For (I = front + 1, maxfreq = queue [front]-> frequency, j = front; I <rear; I ++ ){
- If (queue [I]-> frequency> maxfreq ){
- Maxfreq = queue [I]-> frequency;
- J = I;
- }
- }
- CurLayer = queue [j]-> parent;
- I = 126;
- Path [I] = queue [j]-> letter;
- While (curLayer! = Root ){
- Path [-- I] = curlayer-> letter;
- Curlayer = curlayer-> parent;
- }
- Printf ("% s/n", path + I );
- K ++;
- }
- }
- Void deletetree (trienode * t ){
- If (T! = NULL ){
- Deletetree (t-> child );
- Deletetree (t-> brother );
- Free (t );
- }
- }
- Int main (){
- Int N, W, P, M, I;
- Char word [128], seq [128];
- Root = (TrieNode *) malloc (sizeof (TrieNode ));
- Root-> child = NULL;
- Root-> brother = NULL;
- /* Freopen ("in.txt", "r", stdin );*/
- Scanf ("% d", & n );
- For (I = 1; I <= n; I ++ ){
- Scanf ("% d", & w );
- While (w --){
- Scanf ("% s % d", word, & p );
- Insert (word, p );
- }
- Scanf ("% d", & M );
- Printf ("Scenario # % d:/N", I );
- While (M --){
- Scanf ("% s", SEQ );
- Solve (SEQ );
- Printf ("/N ");
- }
- Deletetree (root-> child );
- Root-> child = NULL;
- Root-> brother = NULL;
- Printf ("/N ");
- }
- Return 0;
- }