HDU dictionary tree parent ent printer

Source: Internet
Author: User

Doing this with others will also be misled. There will be a lot of people who have done this quickly, thinking that sorting and other things will come out, and then they will desperately Think About It, the tragedy is infinite.

It turned out to be the simplest dictionary tree;

Thought: Let's simply imagine that if we want to build all the things to be printed into a dictionary tree, if we want to delete all the last printed things, the number of times we want to walk on the tree is that all sides need to walk twice. But now, after the last printing, no Delete is required, that is to say, the last arrival of the leaf node does not need to be returned to the root node. Therefore, we can put the longest path to reach the leaves at the end (only once ).

 

# Include <string>

# Include <algorithm>

# Include <iostream>

Using namespace STD;

# Define max (A, B) A> B? A: B

# Define min (a, B) a <B? A: B

 

String s [10010];

Struct Node

{

Node * child [26];

// Int CNT;

Bool flag;

Node ()

{

Flag = true;

// CNT = 1;

For (INT I = 0; I <26; I ++)

Child [I] = NULL;

}

};

 

Node * root;

Int ans;

Void solve (string Str)

{

Int Len = Str. Length ();

Node * temp;

Temp = root;

Int I = 0, Pos = 0, index;

While (I <Len)

{

Index = STR [I]-'A ';

If (temp-> child [Index]! = NULL)

{

Temp = temp-> child [Index];

// Temp-> CNT ++;

}

Else

{

Temp-> child [Index] = new node ();

Temp = temp-> child [Index];

}

I ++;

}

}

 

Void finds (string Str)

{

Node * now;

Now = root;

Int I = 0, index, TOT = 0;

Int Len = Str. Length ();

While (I <Len)

{

Index = STR [I]-'A ';

/* If (now-> child [Index]-> CNT = 1)

{

Count ++;

}*/

If (now-> child [Index]-> flag)

{

TOT ++;

Now-> child [Index]-> flag = false;

}

Now = now-> child [Index];

I ++;

}

Ans + = tot * 2;

// Return count;

}

Int main ()

{

Int I, j, N, Len;

String STR;

While (CIN> N)

{

Int MA =-1, temp;

Ans = 0;

Root = new node ();

For (I = 0; I <n; I ++)

{

Cin> S [I];

Len = s [I]. Length ();

If (LEN> Ma)

MA = Len;

Ans ++;

Solve (s [I]);

}

For (I = 0; I <n; I ++)

{

Finds (s [I]);

// Ans + = temp;

// ANS-= (s [I]. Length ()-Temp );

}

Cout <ANS-MA <Endl;

}

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.