Dictionary tree (model body)

Source: Internet
Author: User
N (1 <= N & n <= 2*10 ^ 6) strings are provided. Each string contains only lowercase English letters and a maximum of five letters. Ask the maximum number of occurrences of the N strings. Enter a single group of inputs. Enter a number N in the first line, and then n rows. Each row contains a string. Output a number to indicate the answer. Sample Input
5abaabbwabaz
Sample output
2
Prompt

 

 

 

 

# Include <iostream>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <algorithm>
Const int n= 20010;
Using namespace STD;

Struct Node
{
Int flag;
Node * Next [26];
};

Int n, m, ANS = 0;

Struct node * creat ()
{
Node * P = new node;
For (INT I = 0; I <26; I ++)
{
P-> next [I] = NULL;
}
P-> flag = 0;
Return P;
}


Void insert (node * root, char * B)
{
Int num;
Int Len = strlen (B );

Node * P = root;

For (INT I = 0; I <Len; I ++)
{
Num = B [I]-'A ';
If (p-> next [num] = NULL)
{
P-> next [num] = creat ();
}
P = p-> next [num];

}
P-> flag ++;
If (p-> flag> ans)
Ans = p-> flag;
}

Void free (struct node * root)
{
For (INT I = 0; I <n; I ++)
{
If (root-> next [I]! = NULL)
{
Free (root-> next [I]);
}
}
Free (Root );
}

Int main ()
{
Char a [n] [50], s [50];
Node * P;

Scanf ("% d", & M );
P = creat ();


For (INT I = 0; I <m; I ++)
{
Scanf ("% s", S );
Insert (P, S );
}
Printf ("% d \ n", ANS );
Free (P );
Return 0;
}

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.