4189 Dictionaries
time limit: 1 sspace limit: 256000 KBtopic rank: Master Master SolvingView Run ResultsTitle Description
Description
The most, Skyzhong got a good dictionary, the dictionary full of N words (1<=n<=200000)
Now Skyzhong need to query the dictionary for words that begin with a certain letter.
such as: Skyzhong want to query a
So as long as the word "a" begins.
Skyzhong just wanted to know if there was a word in it (because he didn't check it)
If yes, please output yes. If not, please output no
Enter a description
Input Description
First row one number n
The second line to the N+1 line, one line at a string
The next line is a number m, which indicates the number of times Skyzhong wants to query
Then the M line, a string, represents what Skyzhong wants to check.
Output description
Output Description
A total of M-line, if there is this string output yes, otherwise output no
Sample input
Sample Input
3
Asd
Asfdghj
Asfd
3
Asd
Asdghj
Asf
Sample output
Sample Output
YES
NO
YES
Data range and Tips
Data Size & Hint
The string has only lowercase letters and the length ≤8
Ideas:
Bare Tire tree;
Come on, on the code:
#include <string>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;structTreeNode {BOOLif_[ -]; TreeNode*word[ -];};classTiretreetype {Private: TreeNode*NULL,*Root; InlinevoidInsert_tire (TreeNode *now,Charstr[]) { intlen=strlen (str); for(intI=0; i<len;i++) { if(!now->if_[str[i]-'a']) { now->if_[str[i]-'a']=true; now->word[str[i]-'a']=NewTreeNode; for(intj=0;j< -; j + +) now->word[str[i]-'a']->word[j]=NULL; } Now=now->word[str[i]-'a']; }} inlineBOOLFind_tire (TreeNode *now,Charstr[]) { intlen=strlen (str); for(intI=0; i<len;i++) { if(now->if_[str[i]-'a']) { now=now->word[str[i]-'a']; } Else return false; } return true; } Public: Tiretreetype () {NULL=NewTreeNode; for(intI=0;i< -; i++)NULL->word[i]=NULL; Root=NewTreeNode; for(intI=0;i< -; i++) root->word[i]=NULL; } voidInsert (Charstr[]) {Insert_tire (ROOT,STR); } BOOLFind (Charstr[]) { returnFind_tire (ROOT,STR); }};classTiretreetype Tree;intn,m;Charcgets[Ten];intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%s", cgets); Tree. Insert (cgets); } scanf ("%d",&m); for(intI=1; i<=m;i++) {scanf ("%s", cgets); if(Tree. Find (cgets)) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
AC Diary--Dictionary Codevs 4189