HDU-2846 repository dictionary tree

Source: Internet
Author: User
Repository

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 961 accepted submission (s): 313

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 merchandise 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 queries. 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 input20
Ad
AE
AF
AG
Ah
AI
AJ
AK
Al
Ads
Add
Ade
ADF
ADG
ADH
Adi
Adj
Adk
ADL
AES
5
B
A
D
Ad
S

Sample output0
20
11
11
When I hear this, I am confused. It is true that this problem is quite tangled. yy converts it into numbers to divide it in vain... The brute-force method is to split the given n words into N words, and then perform basic search. Here, you must note that the repeated parts of the same word are ignored. Use the new variables in the structure question to record the words that are overwritten last time on a node.CodeAs follows:

# Include <stdio. h> # include <stdlib. h> # include <string. h> typedef struct node {int flag; int Pos; struct node * child [26];} node; node * Init () {node * n = (node *) malloc (sizeof (node); N-> flag = 0; n-> Pos =-1; memset (n-> child, null, sizeof (n-> child); Return N;} void insert (node * P, char * In, int POS) {If (* In = '\ 0 ') {return;} else {If (p-> child [* In-'a'] = NULL) {P-> child [* In-'a'] = Init ();} I F (p-> child [* In-'a']-> pos! = POS) {P-> child [* In-'a']-> flag ++ ;} p-> child [* In-'a']-> Pos = Pos; insert (p-> child [* In-'a'], in + 1, pos) ;}} int search (node * P, char * In) {If (* In = '\ 0') {If (p-> flag) {return p-> flag;} else {return 0 ;}} else {If (p-> child [* In-'a'] = NULL) {return 0 ;} search (p-> child [* In-'a'], in + 1) ;}} int main () {int p, q, Len; char in [25]; node * n = Init (); scanf ("% d", & P); While (p --) {scanf ("% s", in ); len = strlen (in); For (INT I = 0; I <Len; ++ I) {insert (n, in + I, p );}} scanf ("% d", & Q); While (Q --) {scanf ("% s", in); printf ("% d \ n", search (n, in ));}}

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.