The array of this question needs to reach a million level, such as 1000005, or Re.
Solution: In the dictionary tree, to calculate the number of times the prefix is included at last, you need to add an array record to the number of paths passing through each node when constructing the trie.
// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <string. h> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t ;} template <class T> string TOST Ring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B) ;}# define ifs CIN // template end (General Part) # define maxnode 1000005 # define sigma_size 26 # define str_len 15int ch [maxnode] [sigma_size]; int ch_count [maxnode]; int Val [maxnode]; struct trie {int SZ; trie () {SZ = 1; memset (CH [0], 0, sizeof (CH [0]); memset (ch_count, 0, sizeof (ch_count ));} int idx (char c) {return C-'A';} void insert_trie (char * s, int v) {int u = 0, n = strlen (s ); for (INT I = 0; I <n; I ++) {int C = idx (s [I]); If (! Ch [u] [c]) {memset (CH [SZ], 0, sizeof (CH [SZ]); Val [SZ] = 0; ch [u] [c] = SZ ++;} u = CH [u] [c]; ch_count [u] ++;} Val [u] = V ;} int find_trie (char * s) {int u = 0; int n = strlen (s); int flag = 1; for (INT I = 0; I <N; I ++) {int c = idx (s [I]); If (! Ch [u] [c]) {flag = 0; break;} u = CH [u] [c];} If (flag = 0) {return 0 ;} else {return ch_count [u] ;}}; // hdoj 1251 int main () {// ifstream ifs ("shuju.txt", IOS: In ); char data [str_len]; trie T1; while (IFS. getline (data, str_len, '\ n') & Data [0]! = '\ 0') {t1.insert _ trie (data, 1);} while (IFS> Data & Data [0]! = '\ 0') {int COUNT = t1.find _ trie (data); cout <count <Endl ;}return 0 ;}