UVa 10602 Editor Nottoobad: The idea of equivalent conversion

Source: Internet
Author: User

10602-editor Nottoobad

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1543

Company Macrohard has released it ' s new version of editor Nottoobad, which can understand a few voice. Unfortunately, there are only two voice commands ' It can understand– ' repeat the last word, ' delete the last symbol ' . However, when one uses "repeat" the editor inserts a blank the separates of "the Last word". But The company claims this it is possible to type a much faster–simply by less number of presses. For example, such a phrase like ' This thin thing ' requires only 6 presses of the keyboard.

Action Number
of presses
Content of the document
Press "This" 4 This
Say "Repeat the Last word" 0 This is
Say "Delete the last symbol" 0 This thi
Press "n" 1 This thin
Say "Repeat the Last word" 0 This thin thin
Press "G" 1 This thin thing

In order to increase the popularity of it's product the company decided to organize a contest where the winner would be a P Erson who types a given number of words with minimum number of presses. Moreover, the "the" the "must" is typed, the others can is typed in arbitrary order. So, if words "apple", "Plum" and "apricote" must to be typed, the word "apple" must "typed" and "the words" and "Apricote" can be switched. And the most important for you–you are going to take part in the contest and your have a good friend in the Told you the word which would be used in the contest. You want being a winner J, so your have to write a program which finds the order of the words, where the number of presses WIL L be minimum.

Input

The ' The ' input contains the ' T (1≤t≤15) The number of test cases. Then T test Cases follow. The ' a ' of each test contains a number N (1≤n≤100) –the number of words that must is pressed. Next N lines contain words–sequences of small Latin letters, not longer than. Remember that the ' the ' the ' the ' the ' must be pressed first!

Output

The ' a ' of the output contains number x-the minimum number of presses, which one has to did in order to type all T He words using Editornottoobad. Next N lines contain the words in that minimum order. If There are several solutions, you can output one of them.

Sample Input Sample Output
3
3
This
Thin
Thing
4
Popcorn
Apple
Apricote
Plum
2
Hello
Hello
6
This
Thin
Thing
21st
Popcorn
Plum
Apricote
Apple
5
Hello
Hello

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.