HDU 2846 Repository (build a dictionary tree suffix)

Source: Internet
Author: User

HDU 2846 Repository (build a dictionary tree suffix)

 

Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission (s): 2932 Accepted Submission (s): 1116
Problem Description When 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.
Input There 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.
Output For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
Sample Input
20adaeafagahaiajakaladsaddadeadfadgadhadiadjadkadlaes5badads

Sample Output
02011112

Source 2009 Multi-University Training Contest 4-Host by HDU



Q: How many strings contain keywords?

Question Analysis: For each string, we create a dictionary tree according to its suffix. When creating a tree, we need to add an id to indicate that this branch comes from the first few strings, otherwise it will count again, for example, the third query group in the sample

#include 
 
  #include 
  
   char s[25];int id;struct node{    node *next[26];    int cnt;    int id;    node()    {        memset(next, NULL, sizeof(next));        cnt = 0;        id = -1;    }};void Insert(node *p, char *s, int index, int id){    for(int i = index; s[i] != ''; i++)    {        int idx = s[i] - 'a';        if(p -> next[idx] == NULL)            p -> next[idx] = new node();        p = p -> next[idx];        if(p -> id != id)        {            p -> id = id;            p -> cnt ++;        }    }}int Search(node *p, char *s){    for(int i = 0; s[i] != ''; i++)    {        int idx = s[i] - 'a';        if(p -> next[idx] == NULL)            return 0;        p = p -> next[idx];    }    return p -> cnt;}int main(){    int p, q;    id = 0;    node *root = new node();    scanf(%d, &p);    for(int i = 0; i < p; i++)    {        scanf(%s, s);        int len = strlen(s);        for(int j = 0; j < len; j++)            Insert(root, s, j, i);    }    scanf(%d, &q);    for(int i = 0; i < q; i++)    {        scanf(%s, s);        printf(%d, Search(root, s));    }}
  
 


 

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.