Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1075, string mapping of the dictionary tree.
Test instructions is to give you every Martian word corresponding to the English, and then let you put a Martian article translated into English.
Solution:
Add a string to each end flag in the trie tree, so that you can construct a map for each Martian word. After constructing the map, you can work with the translation section, you can read the line with get, and then handle the line, noting the punctuation. Finally, note that the array is larger.
#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<stack>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64Const intMAXN =500000+5;Const intSigma_size = -;structTrie {intCh[maxn][sigma_size]; Charstr[maxn][ the]; BOOLISEND[MAXN]; intsize; voidinit () {size=1; memset (ch[0] ,0,sizeof(ch[0])); memset (Isend,0,sizeof(Isend)); } intIndexCharc) {returnC'a'; } voidInsertChar*s,Char*S0) { intI, RT; for(i = RT =0; S[i]! =' /'; i++) { intc =index (s[i]); if(!Ch[rt][c]) {memset (ch[size],0,sizeof(Ch[size])); CH[RT][C]= size++; } RT=Ch[rt][c]; } strcpy (Str[rt], s0); ISEND[RT]=1; } Char*find (Char*s) {intI, RT; for(i = RT =0; S[i]! =' /'; i++) { intc =index (s[i]); if(!Ch[rt][c])return "0"; RT=Ch[rt][c]; } return(Isend[rt])? STR[RT]:"0"; }} trie;CharS1[MAXN], S2[MAXN];intMain () {trie.init (); while(~SCANF ("%s", S1)) { if(!STRCMP (S1,"START"))Continue; if(!STRCMP (S1,"END")) Break; scanf ("%s", S2); Trie.insert (S2, S1); } getchar (); while(Gets (S1)) {if(!STRCMP (S1,"START"))Continue; if(!STRCMP (S1,"END")) Break; inti =0; while(S1[i]! =' /') { if(S1[i] >='a'&& S1[i] <='Z') { intj =0; while(S1[i]! =' /'&& S1[i] >='a'&& S1[i] <='Z') {s2[j+ +] = s1[i++]; } S2[j]=' /'; Char*tmp =trie.find (S2); if(tmp[0] =='0') printf ("%s", S2); Elseprintf"%s", TMP); } Else{Putchar (s1[i++]); }} puts (""); } return 0;}
HDU1075 dictionary tree + string map