It took a lot of time to answer this question, and I still want to refer to the idea of Daniel.
Save the dictionary first, and then correspond to each number in the input message, with the prefix with the maximum output frequency. If not, output manually
It mainly applies two aspects of knowledge: The Stack also has a dictionary tree. The implementation of the stack is mainly to save nodes that have not been traversed, so as to facilitate backtracking.
Note the following points:
The first is the frequency of letters in each word, such as "Hell, 3" and "hello, 4 ".Same prefixThe h frequency is 4 + 3 = 7.
The second is to use the stack for backtracking. First, call the find () function to find the maximum frequency prefix for each number in the input string. In the search process, this is actually a deep search, be aware of stack ideas
# Include <iostream> # include <stack> using namespace STD; struct node {int P; // record frequency char C; // record the letter "Node * Next [26]" corresponding to the current node; int lev_number of records}; node * root; void insert (char * s, int P) // insert a word {node * por = root; For (; * s! = '\ 0'; s ++) {int d = * s-'A'; If (por-> next [d] = NULL) {por-> next [d] = new node (); por-> next [d]-> P = P; por-> next [d]-> C = * s;} else {por-> next [d]-> P + = P; // calculate the frequency of the current letter} por = por-> next [d] ;}} void find (char * s, int e) {int table [10] [4] ={{}, {}, {,}, {,}, {6, 7,}, {9, 10, 11,0 },{,}, {,}, {, 24, 25 }}; // The character ing table corresponding to key numbers int max = 0; node * por = root, * cur = root, * NEX; char word [101], ANS [101]; // word records the current letter Prefix: Stack <node *> Nd; ND. Push (cur); cur-> column =-1; while (! Nd. empty () {cur = Nd. top (); nd. pop (); If (cur-> lev>-1) word [cur-> lev] = cur-> C; // record prefix if (cur-> lev== e) {If (cur-> P> = max) // The equal sign cannot be saved. When the frequency is the same, small output in Lexicographic Order. Because the nodes are pushed into the stack, the nodes with large lexicographic orders first access {max = cur-> P; For (INT I = 0; I <= E; I ++) // when the number of layers of the corresponding input string is reached and the frequency is the maximum, assign the prefix saved by the current word to ans {ans [I] = word [I] ;}} else {int n = (s [cur-> lev_+ 1] = '7' | s [cur-> lev_+ 1] = '9 ')? 4: 3; // note the differences between keys 7 and 9 and other numeric keys for (INT I = 0; I <n; I ++) {int d = table [s [cur-> lev+ 1]-'0'] [I]; // s [cur-> lev+ 1] indicates the number corresponding to the next layer, and D indicates the number NEX = cur-> next [d] of the letters mapped to the number key. if (NEX! = NULL) {NEX-> lev= cur-> lev+ 1; // layers of the next node + 1 nd. push (NEX); // push to stack }}} if (max = 0) cout <"manually" <Endl; else {ans [E + 1] = '\ 0'; cout <ans <Endl; // enter the prefix of the maximum frequency} void del (node * por) // clear the dictionary tree {for (INT I = 0; I <26; I ++) {If (por-> next [I]! = NULL) del (por-> next [I]);} Delete por;} int main () {int cas, K = 0, n, m, I, J, p; char word [105], STR [105]; CIN> CAS; while (CAS --) {CIN> N; root = new node (); while (n --) {CIN> word> P; insert (word, p) ;}cout <"Scenario #" <++ k <': '<Endl; CIN> m; while (M --) {CIN> STR; int Len = strlen (STR)-1; for (I = 0; I <Len; I ++) Find (STR, I); cout <Endl;} cout <Endl; del (Root);} return 0 ;}