Job 1058 T9

Source: Internet
Author: User

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:

  1. /* {JOJ1058: T9}
  2. * Date & Time: Thu Oct 09 19:42:13 2008
  3. * File: 1058.c
  4. * Author: WangHaibin
  5. * Platform: windows xp sp2, emacs 22.1, gcc, gdb
  6. *
  7. * Algorithm: Trie tree, wide search
  8. *
  9. * State: Acceptd, 0.00 s, 416 K
  10. **/
  11. # Include <stdio. h>
  12. # Include <stdlib. h>
  13. # Include <string. h>
  14. Typedef struct _ Tn {
  15. Char letter;
  16. Int frequency;
  17. Char belongto;
  18. Struct _ Tn * child, * brother, * parent;
  19. Char iscomplete;
  20. } Trienode;
  21. Trienode * root;
  22. Char belongto [26] = {'2', '2', '2', '3', '3', '4', '4 ', '4', '5', '5', '5', '6', '6', '6 ',
  23. '7', '7', '7', '7', '8', '8', '8', '9', '9', '9 ', '9 '};
  24. Void insert (const char * word, int freq ){
  25. Trienode * P = root, * oldbrother = NULL, * DEST = root-> child;
  26. Int K = 0, Len = strlen (Word );
  27. For (k = 0; k <Len; k ++ ){
  28. While (DEST! = NULL & DEST-> letter! = Word [k]) {
  29. Oldbrother = DEST, DEST = DEST-> brother;
  30. }
  31. If (DEST ){
  32. DeST-> Frequency + = freq;
  33. } Else {
  34. /* DEST is null, word [0] Not found */
  35. DeST = (trienode *) malloc (sizeof (trienode ));
  36. DeST-> letter = word [k];
  37. DeST-> frequency = freq;
  38. DeST-> parent = P;
  39. DeST-> brother = NULL;
  40. DeST-> child = NULL;
  41. DeST-> belongto = belongto [word [k]-'a'];
  42. DeST-> iscomplete = 0;
  43. If (oldbrother ){
  44. Oldbrother-> brother = DEST;
  45. } Else {
  46. /* If the value of oldbrother is null, the subtree of P is empty */
  47. P-> child = DEST;
  48. }
  49. }
  50. P = DEST;
  51. DeST = p-> child;
  52. Oldbrother = NULL;
  53. }
  54. P-> iscomplete = 1;
  55. }
  56. Void solve (const char * SEQ ){
  57. Trienode * queue [1024];
  58. Int front = 0, rear = 0, K = 0, I, j, maxfreq;
  59. Trienode * curlayer;
  60. Char path [128] = {0 };
  61. Queue [rear ++] = root;
  62. While (SEQ [k]! = '1 '){
  63. I = rear-front;
  64. For (j = 1; j <= I; j ++ ){
  65. Curlayer = queue [Front]-> child;
  66. Front ++;
  67. While (curlayer ){
  68. If (curlayer-> belongto = seq [k]) {
  69. Queue [rear ++] = curlayer;
  70. }
  71. Curlayer = curlayer-> brother;
  72. }
  73. }
  74. If (front = rear ){
  75. /* Queue is empty */
  76. Printf ("manually/N ");
  77. K ++;
  78. Continue;
  79. }
  80. For (I = front + 1, maxfreq = queue [front]-> frequency, j = front; I <rear; I ++ ){
  81. If (queue [I]-> frequency> maxfreq ){
  82. Maxfreq = queue [I]-> frequency;
  83. J = I;
  84. }
  85. }
  86. CurLayer = queue [j]-> parent;
  87. I = 126;
  88. Path [I] = queue [j]-> letter;
  89. While (curLayer! = Root ){
  90. Path [-- I] = curlayer-> letter;
  91. Curlayer = curlayer-> parent;
  92. }
  93. Printf ("% s/n", path + I );
  94. K ++;
  95. }
  96. }
  97. Void deletetree (trienode * t ){
  98. If (T! = NULL ){
  99. Deletetree (t-> child );
  100. Deletetree (t-> brother );
  101. Free (t );
  102. }
  103. }
  104. Int main (){
  105. Int N, W, P, M, I;
  106. Char word [128], seq [128];
  107. Root = (TrieNode *) malloc (sizeof (TrieNode ));
  108. Root-> child = NULL;
  109. Root-> brother = NULL;
  110. /* Freopen ("in.txt", "r", stdin );*/
  111. Scanf ("% d", & n );
  112. For (I = 1; I <= n; I ++ ){
  113. Scanf ("% d", & w );
  114. While (w --){
  115. Scanf ("% s % d", word, & p );
  116. Insert (word, p );
  117. }
  118. Scanf ("% d", & M );
  119. Printf ("Scenario # % d:/N", I );
  120. While (M --){
  121. Scanf ("% s", SEQ );
  122. Solve (SEQ );
  123. Printf ("/N ");
  124. }
  125. Deletetree (root-> child );
  126. Root-> child = NULL;
  127. Root-> brother = NULL;
  128. Printf ("/N ");
  129. }
  130. Return 0;
  131. }

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.