HDU-1298-T9
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1298
A good question: dictionary tree + DFS. For more information, see swm8023.
Simulate the mobile phone input method, give a few words, that is, frequency, and then give several numeric strings to determine the characters that will be displayed for each input number for a given number string
Each number in the number string in this question represents a letter, rather than a regular mobile phone. Multiple numbers may represent a letter. First, you can record the words that are given, that is, the frequency, to the dictionary tree, perform DFS for numeric strings and find the string with the highest frequency that it may represent
For example, AB 2
BC 3
23
The string represented by 23 May be ad, AE, AF, BD, Be, BF, CD, Ce, CF, Which is searched one by one to find the most frequently-used string
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> using namespace STD; struct node {int count; node * Childs [26]; node () {COUNT = 0; For (INT I = 0; I <26; I ++) Childs [I] = NULL ;}; node * root; node * Current, * newnode; int P; char S1 [105], S2 [105], find [105], ANS [105]; char phone [8] [4] =, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}, {12, 13, 14}, {15, 16, 17, 18}, {19, 20, 21 }, {, 24, 25 }}; int num [8] = {3, 3, 3, 3, 4, 3}; void INS ERT (char * STR, int K) // insert a string and its frequency {int I, m; current = root; for (I = 0; I <strlen (STR ); I ++) {M = STR [I]-'A'; If (current-> Childs [m]! = NULL) {current = Current-> Childs [m]; (current-> count) + = K;} else {newnode = new node; (newnode-> count) + = K; Current-> Childs [m] = newnode; current = newnode ;}} void DFS (INT cur, int Len, node * Nd) {If (cur = Len) {If (Nd-> count> P) // {P = Nd-> count; For (INT I = 0; I <Len; I ++) Find [I] = ans [I]; find [Len] = '\ 0';} return ;} int T = S2 [cur]-'2'; For (INT I = 0; I <num [T]; I ++) {int r = phone [T] [I]; If (Nd-> Childs [R] = NULL) continue; ans [cur] = 'A' + R; DFS (cur + 1, Len, Nd-> Childs [R]);} return;} int main () {int I, n, m, T, K, CNT, len; scanf ("% d", & T); For (k = 1; k <= T; k ++) {printf ("Scenario # % d: \ n ", k); scanf (" % d ", & N); root = new node; while (n --) {scanf (" % S % d ", S1, & CNT); insert (S1, CNT);} scanf ("% d", & M); While (M --) {scanf ("% s", S2 ); len = strlen (S2); for (I = 1; I <Len; I ++) {P = 0; // records the occurrence frequency of characters in DFS (0, I, root); If (P> 0) printf ("% s \ n", find); elseprintf ("manually \ n");} printf ("\ n ");} printf ("\ n");} return 0 ;}