Statistical puzzles
Time limit:4000/2000 MS (java/others) Memory limit:131070/65535 K (java/others)
Total submission (s): 36675 Accepted Submission (s): 13637
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 OUTPUT2310AUTHORIGNATIUS.L point to find the tree: 1. The root node is an empty point, only the formal starting point function (insert , starting from it when looking for) 2. Skilled application of pointers to avoid excessive debug pointer type
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;structnode{intKBOOLIFT; Node*ch[ -];}*Root;node*Create () {node*d=New(node); memset (d->ch,0,sizeof(d->ch)); D->k=0; D->ift=0; returnD;}voidInsertChar*s) {Node*q=Root; Char*p=s; while(*p) {intid=*p-'a'; if(q->ch[id]==null) q->ch[id]=Create (); Q=q->Ch[id]; P++; Q->k++; } q->ift=1;}intSearchChar*R) {Node*q=Root; Char*p=s; while(*p) {intid=*p-'a'; Q=q->Ch[id]; P++; if(Q==null)return 0; } returnQ->K;}intMain () {root=create ();Chars[ A]; BOOLR=0; while(gets (s)) {if(strlen (s) = =0) Break; Insert (s); } while(Gets (s)) Cout<<search (s) <<Endl;}View Code
Array emulation
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespacestd;inttrie[400001][ -],len,root,tot,sum[400001];BOOLp;Chars[ One];voidInsert () {len=strlen (s); Root=0; for(intI=0; i<len;i++) { intid=s[i]-'a'; if(!trie[root][id]) trie[root][id]=++tot; Sum[trie[root][id]]++; Root=Trie[root][id]; }}intSearch () {root=0; Len=strlen (s); for(intI=0; i<len;i++) { intid=s[i]-'a'; if(!trie[root][id])return 0; Root=Trie[root][id]; } returnsum[root];}intMain () { while(gets (s)) {if(!p) {if(strlen (s)) insert (); Elsep=1; } Elseprintf"%d\n", search ()); }}View Code
Trie tree Templates (statistical puzzles)