RepositoryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission (s): 1750 Accepted Submission (s): 651 Problem DescriptionWhen you go shopping, you can search in repository for avalile merchandises by the computers and internet. first you give the search system a name about something, then the system responds with the results. now you are given a lot mercha Ndise names in repository and some queries, and required to simulate the process. inputThere is only one case. first there is an integer P (1 <= P <= 10000) representing the number of the merchanidse names in the repository. the next P lines each contain a string (it's length isn't beyond 20, and all the letters are lowercase ). then there is an integer Q (1 <= Q <= 100000) representing the number of the queri Es. the next Q lines each contains a string (the same limitation as foregoing descriptions) as the searching condition. outputFor each query, you just output the number of the merchandises, whose names contain the search string as their substrings. sample Input20adaeafagahaiajakaladsaddadeadfadgadhadiadjadkadlaes5badads Sample Output02011112 Source2009 Multi-University Training Contest 4-Host HDU Recommendgaojie: the third bullet in the dictionary tree, which requires us to find the substring of the number of words in the dictionary, not just its prefix, therefore, words in the dictionary need to be decomposed into different prefixes and added to the tree, that is, abc words are split into three forms: abc, bc, and c, this is equivalent to looking for the prefix of the number of words in the dictionary. It should be noted that for multiple prefixes decomposed by the same word, if they are the same, this prefix is only counted as one type. Words such as abca are decomposed into two prefixes: abca and, so it is regarded as one type. To determine whether the same word is decomposed, you need to add the same id to each input word and its decomposed prefix. [Cpp] # include <cstdio> # include <cstring> # include <iostream> using namespace std; int ans, len; typedef struct TrieNode {int c, id; struct TrieNode * next [26]; TrieNode () {id =-1; // initialize the id of each node to-1 c = 0; memset (next, 0, sizeof (next) ;}}; TrieNode * root = NULL; void CreatTree (char s [], int num) {int I, len; len = strlen (s ); trieNode * p = root; TrieNode * temp; for (I = 0; I <len; I ++) {www.2cto.com if (p-> next [s [I]-'a'] = NULL) {temp = new TrieNode; p-> next [s [I]-'a'] = temp ;} p = p-> next [s [I]-'a']; if (p-> id! = Num) // If the prefix of the decomposed word is the same as that of the word that has been previously decomposed (id = num ), then it cannot be regarded as a new prefix p-> c ++; p-> id = num; // the word that is decomposed into the same word, give the same id} void Search (char s [], int d) {int I, j; TrieNode * p = root; for (I = 0; I <d; I ++) {if (p-> next [s [I]-'a']) p = p-> next [s [I]-'a']; else {ans = 0; // If the entered word is not the prefix of any word in the dictionary and Its decomposed word, the word is not the return ;}} ans = p-> c; // otherwise, in the words decomposed by different words (repeated words are not counted, the prefix of the output word is the expected answer} void Delete (TrieNode * node) {int I; for (I = 0; I <26; I ++) {if (node-> next [I]) Delete (node-> next [I]); delete node-> next [I]; node-> next [I] = 0 ;}} int main () {int I, j, m, n, len; char str [25], temp [25]; root = new TrieNode; scanf ("% d", & n); memset (str, '\ 0', sizeof (str); for (I = 0; I <n; I ++) {scanf ("% s", & str); len = strlen (str); for (j = 0; j <len; j ++) {strncpy (temp, str + j, len-j); // separate words in the dictionary with different prefixes, add temp [len-j] = '\ 0' to the tree; CreatTree (temp, I ); // I is used to determine whether it is a memset (str, '\ 0', sizeof (str);} scanf ("% d ", & m); for (I = 0; I <m; I ++) {www.2cto.com ans = 0; scanf ("% s", & str ); len = strlen (str); Search (str, len); printf ("% d \ n", ans);} Delete (root); return 0 ;}