Title Link: https://www.facebook.com/hackercup/problems.php?pid=313229895540583&round=344496159068801
The main idea: see for yourself (in fact I don't know)
Bare trie tree, directly see if there is a node that must be inserted.
The code is too lame. Let's see.
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <vector>5#include <cstdlib>6 using namespacestd;7 #defineCHARSET 268 9 Const intMax_node =10000000;Ten One structtrienode{ A intCh[charset]; - }; - Trienode Trie[max_node]; the intptr; - intans; - - intT,n; + CharStr[max_node]; - + A voidinit () { atptr = ans =0; - for(intI=0; i<charset;i++){ -trie[0].ch[i] =-1; - } - } - in intNewNode () { -ptr++; to for(intI=0; i<charset;i++){ +Trie[ptr].ch[i] =-1; - } the returnptr; * } $ Panax Notoginseng voidInsertConst Char*str) { - BOOLhasadded =false; the intLen =strlen (str); + intRT =0; A for(intI=0; i<len;i++){ the intid = str[i]-'a'; + if(trie[rt].ch[id]==-1 ){ -Trie[rt].ch[id] =NewNode (); $ if( !hasadded) { $Ans = ans + i +1; -hasadded =true; - //printf ("+%d\n", i+1); the } - }WuyiRT =Trie[rt].ch[id]; the } - if(!hasadded) { WuAns = ans +Len; - //printf ("+%d\n", Len); About } $ } - - intMain () { - //freopen ("Input.txt", "R", stdin); A //freopen ("Output.txt", "w", stdout); +scanf"%d",&T); the for(intCases =1; cases <= T; cases++){ -scanf"%d",&N); $ init (); the for(intI=0; i<n;i++){ thescanf"%s", str); the Insert (str); the } -printf"Case #%d:%d\n", Cases,ans); in } the return 0; the}
[Hackercup Round1 2] Autocomplete (Trie)