Nyoj 290 dictionary tree

Source: Internet
Author: User

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 290

Hash can also be used because the test data is large. However, note that the effects of CIN and cout on time are relatively large. Therefore, replace it with scanf and printf.

First, we will introduce the dictionary tree (tire) method.

The dictionary tree adopts a space-for-time strategy.

# Include <iostream>
# Include <string>
# Include <cstring>
Using namespace STD;
Struct Node
{
Int sum; // number of occurrences
Node * Next [26];
};
Int search (char * s, node * t)
{
Node * P, * q;
P = T;
Int I = 0, ID, J;
While (s [I])
{
Id = s [I]-'A ';
If (p-> next [ID] = NULL) // if the branch is empty, set the node
{
Q = new node;
Q-> sum = 0;
For (j = 0; j <26; ++ J)
Q-> next [J] = NULL;
P-> next [ID] = Q;
}
P = p-> next [ID]; // continue searching along the branch
++ I;
}
(P-> sum) ++;
Return p-> sum; // The number of times the string appears at the end.
}
Int main ()
{
Int N, I, max = 0;
Node * t = new node;
For (I = 0; I <26; ++ I) {T-> next [I] = NULL; t-> sum = 0 ;}
Char temp [50], name [50];
Cin> N;
For (I = 0; I <n; ++ I)
{
Cin> temp;
Int num = search (temp, t );
If (Num> MAX)
{
Max = num;
Strcpy (name, temp );
}
}
Cout <name <"" <max <Endl;
System ("pause ");
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.