AC Diary--Dictionary Codevs 4189

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.