structTrie { int num; Trie* next[26]; Trie() { for (int i = 0; i <= 25; i++) next[i] = NULL; num = 0; } };
Trie *root = new Trie;
voidInsert(char* word){ Trie* p = root; for (int i = 0; word[i]; i++) { if (p->next[word[i] - 'a'] == NULL) { p->next[word[i] - 'a'] = new Trie; } p = p->next[word[i] - 'a']; p->num++; } }
voidFreedom(Trie *p){ for (int i = 0; i < 26; i++) { if (p -> next[i] != NULL) Freedom(p -> next[i]); } delete p; }
intFind(char* word){ Trie* p = root; for (int i = 0; word[i]; i++) { if (p->next[word[i] - 'a'] == NULL) { return0; } p = p->next[word[i] - 'a']; } return p->num; }
intmain(){ charword[11]; int n, m; while (cin.getline(word, 12)) { if (strlen(word) == 0) { break; } Insert(word); } while (scanf("%s", word) != EOF) { printf("%d\n", Find(word)); } Freedom(root); return0; }
voidInsert(char *word){ int len = strlen(word); int root = 0; for (int i = 0; i < len; i++) { int id = word[i] - '0'; if (!tree[root][id]) tree[root][id] = ++tot; root = tree[root][id]; flagg[root]++; } //flagg[root] += 1; }
intFind(char* word){ int len = strlen(word); int root = 0; int ans = 0; for (int i = 0; i < len; i++) { int id = word[i] - '0'; if (!tree[root][id]) return0; //if (flagg[tree[root][id]]) //ans += 1; root = tree[root][id]; } ans = flagg[root]; return ans; }
voidinit(){ for (int i = 0; i <= tot; i++) { flagg[i] = false; for (int j = 0; j < 30; j++) { tree[i][j] = 0; } } tot = 0; }
intmain(){ charword[11]; int n, m; init(); while (cin.getline(word, 12)) { if (strlen(word) == 0) { break; } Insert(word); } while (scanf("%s", word) != EOF) { printf("%d\n", Find(word)); } return0; }