Hdu3460 principal ent printer

Source: Internet
Author: User

Alas, I contributed a wa again and forgot to delete my output for testing ......

At the beginning, I was really scared by this question. However, I can still do it if I think about it.

The question requires the minimum number of operations for printing words.

There are only three operations:

Input, delete, and print. The prefix does not need to be input repeatedly. When you think of the dictionary tree, this question is easy to do.

To obtain the least number of operations, you must first classify all words with the same prefix and put them in one input. The question also says that the last print does not need to be deleted.

In theory, it is like what we mentioned above, but when we calculate the least operand, we can calculate it like this. First, assume that all words need to be re-printed and deleted, so the total operand is the total length of the word * 2

Next, calculate the prefix. In this way, you only need to calculate which words are common to each node, such as the example in the question, "freeradiant", "freeopen ", f is shared by two nodes, saving the need for One-Step Deletion and one-step input, so it is (2-1) * 2;

It is easy to understand that the last line does not need to be deleted. The last line must be longer and better,

Based on these three values, we can find that the minimum operation is the total number of operations-the total number of operations that can be saved-the length of the last row deleted multiple times (max)

InCodeImplementation, amount, 125 Ms, not very fast, still use the stack and dictionary tree

For more details, see the code.

# Include <iostream> # include <stack> # include <string> using namespace STD; struct node {node * Next [26]; int V; // record the number of words that have passed through the node, or the number of words that have the prefix int lev_number until the node; // the number of layers does not seem very useful }; node * root; void insert (char * s) // insert operation {node * P = root; For (; * s! = '\ 0'; s ++) {int d = * s-'A'; If (p-> next [d]! = NULL) {P = p-> next [d]; P-> V ++;} else {P-> next [d] = new node (); P = p-> next [d]; P-> V ++ ;}} int find () // traverse the entire tree {node * P = root, * cur = root, * NEX; int sum = 0; stack <node *> Nd; cur-> lev= 0; nd. push (cur); While (! Nd. empty () {cur = Nd. top (); nd. pop (); If (cur-> lev> 0) // The root node does not calculate {sum + = (cur-> v-1) * 2;} For (INT I = 0; I <26; I ++) {NEX = cur-> next [I]; If (NEX! = NULL & NEX-> V> 1) // if the number of words is greater than one, it indicates that an operation can be saved before calculation is necessary, {NEX-> lev= cur-> lev+ 1; nd. push (NEX); // push to stack }}return sum;} void del (node * P) // release space {for (INT I = 0; I <26; I ++) {If (p-> next [I]! = NULL) del (p-> next [I]);} Delete (p);} int main () {int sum, I, j, N, Max, Len; char STR [55]; while (scanf ("% d", & N )! = EOF) {sum = max = 0; root = new node (); for (I = 0; I <n; I ++) {scanf ("% s ", str); insert (STR); Len = strlen (STR); If (LEN> MAX) // find the longest word max = Len; sum + = Len; // calculate the total word length} J = find (); sum = sum * 2-j-max + N; cout <sum <Endl; del (Root );}}

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.