Statistical puzzles
Time limit:4000/2000 MS (java/others) Memory limit:131070/65535 K (java/others)
Total submission (s): 31479 Accepted Submission (s): 12087
Problem Descriptionignatius recently encountered a problem, the teacher gave him a lot of words (only lowercase letters, no duplicate words appear), now the teacher asked him to count the number of words prefixed with a string (the word itself is also its own prefix).
The first part of input data is a Word table, one word per line, the length of the word is not more than 10, they represent the word that the teacher handed to Ignatius, and a blank line represents the end of the Word table. The second part is a series of questions, one question per line, and each question is a string.
Note: There is only one set of test data, processing to the end of the file.
Output for each question, give the number of words prefixed with the string.
Sample INPUTBANANABANDBEEABSOLUTEACMBABBANDABC
Sample Output2 3 1 0 The typical application of a dictionary tree is to write the process of making achievements and finding trees. When the tree is used DFS I found that with the pointer to write a tree is very good to write, so with pointers, haha, the following is the code, the dictionary tree can be saved as a template, this problem let me long knowledge of the input and output limitations, and C + + and g++ memory management of the wit is not the same, the problem is to use the g++ In C + + cross over and then enter the time to summarize: because it is scanf ("%d", &TM) is not read into the last carriage return, and scanf ("%c", &ch) will read the carriage return, so after%d if the carriage return to read into the%c, add GetChar () scanf ("%s", STR) is not read into the carriage return, it automatically ignores the carriage return and stops at the carriage return. So if the input needs to be read into the return, you need to use gets (); This problem can also be used in the set method, because the string does not exceed 10, so you can directly put all the prefixes of each string into the set and then processing the dictionary tree method:
1#include <cstdio>2#include <cstring>3#include <malloc.h>4#include <iostream>5 using namespacestd;6 #defineMAXN 267typedefstructTrie {8 intV//Change as needed9Trie *NEXT[MAXN];Ten //Next is how many kinds of numbers are in each layer, and if they are just lowercase letters, then 26, One //If you change the case to uppercase and lowercase, it is 52, and if you add a number, it is 62. A } Trie; - Trie Root; - the voidCreatetrie (Char*str) { - intLen =strlen (str); -Trie *p = &root, *Q; - for(inti =0; i < Len; i++) { + intid = str[i]-'a'; - if(P->next[id] = =NULL) { +Q = (Trie *)malloc(sizeof(root)); AQ->v =1;//Initial v==1 at for(intj =0; J < Maxn; J + +) -Q->NEXT[J] =NULL; -P->next[id] =Q; -p = p->Next[id]; -}Else { -p->next[id]->v++; inp = p->Next[id]; - } to } + //p->v =-1;//if at the end, change V to 1 to indicate - } the * intFindtrie (Char*str) { $ intLen =strlen (str);Panax NotoginsengTrie *p = &Root; - for(inti =0; i < Len; i++) { the intid = str[i]-'a'; +p = p->Next[id]; A if(p = = NULL)//if it is an empty set, the string that is prefixed by this is not saved the return 0; + //if (p->v = =-1)//a string already in the character set is the prefix of this string - //return-1; $ } $ returnP->v; - //return-1; //This string is the prefix of a string in the character set - } the intDealtrie (trie*T) { - //Dynamic dictionary tree, sometimes hyper-memory, it is necessary to remember to free up spaceWuyi if(t==NULL) the return 0; - for(inti =0; i < MAXN; i++) { Wu if(t->next[i]!=NULL) -Dealtrie (t->next[i]); About } $ Free(T); - return 0; - } - intMain () { A Charstr[ the]; + for(inti =0; i < MAXN; i++) theRoot.next[i] =NULL; - while(Gets (str) && str[0]!=' /') $ Createtrie (str); thememset (str,0,sizeof(str)); the while(SCANF ("%s", str)! =EOF) { the intAns =Findtrie (str); theprintf"%d\n", ans); - } in return 0; the}
Set Method:
1#include <cstdio>2#include <iostream>3#include <map>4#include <cstring>5#include <string>6 using namespacestd;7 8 intMain ()9 {Ten Charstr[ -]; Onemap<string,int>m; A while(gets (str)) - { - intLen =strlen (str); the if(!len) - { - Break; - } + for(inti = len; i >0; i--) - { +Str[i] =' /'; Am[str]++; at } - } - while(gets (str)) - { -cout<<m[str]<<Endl; - } in - return 0; to}
hdu_1251 Statistical Puzzles (Dictionary tree trie)