Topic Links:
http://hihocoder.com/problemset/problem/1014
Test instructions
Given n words, and then we make a dictionary tree, and then give you M-strings, ask how many words are prefixed with this string.
The code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int MAXN = 100010;const int max_size = 30;int ID (char c) {return C ' a ';} struct trie{Trie *ch[max_size]; int num; Trie () {num = 0; for (int i=0;i<max_size;i++) ch[i]=null; }}*root;void insert_str (char *s) {Trie *p = root; p->num++; for (int i=0; p&&s[i]; i++) {int u = ID (s[i]); if (p->ch[u]==null) p->ch[u] = new Trie; p=p->ch[u]; p->num++; }}int find_str (char *s) {Trie *p = root; for (int i=0;p&&s[i];i++) {int u = ID (s[i]); if (P->ch[u]==null) return 0; p=p->ch[u]; } return p->num;} int main () {char s[12]; int n, m; while (~SCANF ("%d", &n)) {root = new Trie; while (n--) {scanf ("%s", s); INSERT_STR (s); } scanf ("%d", &m); while (m--) {scanf ("%s", s); printf ("%d\n", Find_str (s)); }} return 0;}
Dictionary tree Exercises (i) Hihocoder 1014 (number of identical prefixes)