original title link:http://acm.hdu.edu.cn/showproblem.php?pid=1251
Test Instructions: first give some words to form a dictionary, and then look at some of the words in the dictionary prefixed with some alphabetic strings.
Analysis: very basic dictionary tree problem, but with the dynamic dictionary tree will time out, so use a static dictionary tree.
Code:
1#include <Set>2#include <map>3#include <list>4#include <cmath>5#include <queue>6#include <vector>7#include <bitset>8#include <string>9#include <cctype>Ten#include <cstdio> One#include <cstring> A#include <cstdlib> -#include <iostream> -#include <algorithm> the - using namespacestd; - -typedefLong Longll; +typedef unsignedLong Longull; - #defineINF (0X3F3F3F3F) + #defineLNF (0x3f3f3f3f3f3f3f3f) A #defineEPS 1e-8 at intSgnDoubleA) {returnA <-eps? -1: A < EPS?0:1;} - - //-------------------------- - - structTrie_node { - intCNT; in intnext[ -]; -}tree[400010]; to intNXT; + - intCreatetrienode () { thememset (&TREE[NXT],0,sizeof(Trie_node)); * returnnxt++; $ }Panax Notoginseng - voidTrie_insert (string&word) { the intrt=0; + intlen=word.length (); A for(intI=0; i<len;i++){ the intid = word[i]-'a'; + if(!Tree[rt].next[id]) { -tree[rt].next[id]=Createtrienode (); $ } $rt=Tree[rt].next[id]; -tree[rt].cnt++; - } the } - Wuyi intTrie_search (string&word) { the intrt=0; - intlen=word.length (); Wu for(intI=0; i<len;i++){ - intid=word[i]-'a'; About if(!tree[rt].next[id])return 0; $rt=Tree[rt].next[id]; - } - returntree[rt].cnt; - } A + stringWord; the - voidinit () { $ the } the the voidsolve () { thenxt=1; -memset (&tree[0],0,sizeof(Trie_node)); in while(Getline (Cin,word)) { the if(word=="") Break; the Trie_insert (word); About } the while(Getline (Cin,word)) { theCout<<trie_search (word) <<Endl; the } + } - the intMain () {Bayi the #ifndef Online_judge theFreopen ("In.txt","R", stdin); - //freopen ("OUT.txt", "w", stdout); - #endif theIostream::sync_with_stdio (false); the init (); the solve (); the return 0; -}
(dictionary tree) HDU-1251 Statistical puzzles