1123: Statistical difficulties (Dictionary tree), 1123 dictionaries

Source: Internet
Author: User

1123: Statistical difficulties (Dictionary tree), 1123 dictionaries

1123: Statistical difficulty time limit: 1 Sec memory limit: 128 MB
Submitted: 4 solution: 4
[Submit] [Status] [discussion version] Description

Ignatius encountered a problem recently. The teacher gave him many words (only lowercase letters are used, and no duplicate words will appear ), the teacher asks him to calculate the number of words prefixed with a certain string (the word itself is also its own prefix ).

Input

The first part of the input data is a word table with one word per row. The length of each word cannot exceed 10. They represent the words that the teacher gave to Ignatius for statistics, and an empty row indicates the end of the word table. the second part is a series of questions. Each question in each row is a string.

Note: This question only contains a set of test data, which is processed until the end of the file.

Output

For each question, the number of words prefixed with this string is given.

Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample output
2310
The basic questions of the dictionary tree have already been written into several dictionary tree questions. The idea is also clear. First, create a dictionary tree and then count the number of prefixes; this is the input format. Note that the others are relatively simple. It is a dictionary entry question. Use your previous template directly;

The following is the ac code:

#include <cstdio>#include <cstring>#include <cstdlib>typedef struct node // Structure of the node{    int count;// Count    struct node * next[26];}node;node * newnode() // Create a node{    node *q;    q=(node*)malloc(sizeof(node));    q->count=1;    for(int i=0;i<26;i++)        q->next[i]=NULL;    return q;}void build(node *T,char *s) // Create a dictionary tree{    node *p;    p=T;    int len,k;    len=strlen(s);    for(int i=0;i<len;i++)    {        k=s[i]-'a';        if(p->next[k]==NULL)        {            p->next[k]=newnode();            p=p->next[k];        }        else        {            p=p->next[k];            p->count++;        }    }}int search(node *T,char *c) // Query the dictionary tree{    node *q;    int sum=0,len,k;    q=T;    len=strlen(c);    for(int i=0;i<len;i++)    {        k=c[i]-'a';        if(q->next[k]!=NULL)          q=q->next[k];        else          return sum=0;    }    sum=q->count;    return sum;}int main(){    char s[12];    char c[12];    node *T;    T=(node *)malloc(sizeof(node));    T->count=0;    for(int i=0;i<26;i++)        T->next[i]=NULL;    memset(s,0,sizeof(s));    memset(c,0,sizeof(c));    while(gets(S) // control input note    {       if(strcmp(s,"")==0)        break;       build(T,s);    }    while(scanf("%s",c)!=EOF)    {        printf("%d\n",search(T,c));    }    return 0;}

How to Create a dictionary tree

What's on the tree?
There is an dictionary on the book
 
A beginner in programming wants to know about the spatial complexity analysis of the dictionary tree. Can someone help me?

Dictionary tree (Trie tree ),
The Trie tree is a dictionary tree. Its core idea is to change the space for time.

For a simple example.

The length is 0.1 million characters and cannot exceed 10 characters. For each word, we need to judge whether it comes and goes. If there are, there are several positions for the first time.
Of course, hash can be used for this problem, but I want to introduce the trie tree for you. It is used more in some aspects. For example, for a word, I want to ask whether it is a prefix. Such a hash value is very simple if it is not used in Terry.
Now, for example, if we use the dumbest method for each word, we have to look for whether the word above it is. The complexity of this algorithm is O (n ^ 2 ). Obviously, it is unacceptable for the range of 100000. Now, we need to change our thinking. Suppose I have to find the word "ABCD". Then, when the word "B", "C", "D", "F" and "image" are in front of him, I obviously do not consider it. You only need to find out whether there is a beginning of ABCD. Similarly, for the words at the beginning, as long as we take into account the second letter of B... the model of this tree is becoming clearer ......
Let's assume that B, ABC, ABD, BCD ABCD, EFG, and HII are six characters, and we create a tree.

The process of traversing each node is essentially a word. This word is considered to exist or not exist if the node is marked in red.
Well, I just followed the corresponding node and it appeared before it was marked red. This node is marked in red, which is equivalent to the word being inserted.
So the time we requested was only the length and insertion of the word. In the sample, there were 10.
We can see that the nodes in each layer of the trie tree are 26 ^ I ,. Therefore, in order to save space. We use a dynamic link list or a simulated dynamic array. The number of words.

Dictionary tree (Terry) is a fast string search multi-tree structure. The principle is to use a common prefix string to reduce the overhead of time and space, thus improving the program efficiency.

It has the following simple features: (1) the root node contains character information; (3) the m feature is empty, or the M feature of the rice tree. Query items: (1) start searching from the root node; (2) obtain the keywords to be searched, select the corresponding subtree, and continue searching based on the subtree in the letter (3) in the corresponding subtree, You need to search for the keyword and select the second letter from the corresponding subtree. (4) The iteration process... (5) at an intersection, all the letters of the deleted keywords, and the nodes connected to the read information, that is, the search is completed.

Attach the AC code to the landlord for good learning
# Includes
# Includes
# Medium

Structured dictree
{
Structure dictree * sub [26];
Integer N;
} Dictree * root;

Invalid insertion (CHAR * Source)
{
INT lens, I, J;
Dictree * Current * newnode; BR/> length = strlen (source );
(LEN = 0) Return
Current = root;
(I = 0; <LEN, I ++ ){
(Current> sub [I]-[Source '1'] = 0 ){
Current = Current> subsource [I]-'a'];
CURRENT-> N = CURRENT> N + 1;
}
Others {
Newnode = (dictree *) malloc (sizeof (structured dictree;
(J = 0; J <26; ++)
Newnode-> sub [J] = 0;
Current> sub [I]-[Source '1'] = newnode;
Current = newnode;
Current> N = 1;
}
}
}
INT (character * Source)
{
Me, LEN;
Structure dictree * current;
Length = strlen (source );
(LEN = 0) returns 0;
Current = root;... the remaining full text>
 

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.