QOJ.ac

QOJ

Límite de tiempo: 3 s Límite de memoria: 1024 MB Puntuación total: 100

#14758. Web browser

Estadísticas

Bajtosia is preparing a presentation for her computer science class. To do this, she must visit $n$ websites with pairwise distinct addresses to gather the necessary information.

The web browser Bajtosia uses has a text field that initially contains an empty string. By pressing keys, she can modify the content of the field and visit the website whose address corresponds to the string currently in the text field. The following operations are available:

  1. Keys from a to z append the pressed letter to the end of the current string.
  2. The BACKSPACE key removes the last letter of the current string (does nothing if the string is empty).
  3. The ENTER key visits the website with the address corresponding to the current string, and then clears the string in the text field (i.e., changes it to an empty string).
  4. The TAB key auto-completes the current string to the most recently visited website among those for which the current string is a prefix (i.e., the initial fragment). It does nothing if no such website has been visited.

There is little time left to finish the presentation, so Bajtosia wants to visit all the required websites using the minimum possible number of keystrokes. Bajtosia cannot visit any websites other than the required ones. Bajtosia can visit the required websites in any order, and each must be visited exactly once. Help Bajtosia by determining this minimum number of keystrokes and the sequence of keys to press.

Input

The first line of input contains a single integer $n$ ($1 \le n \le 10^6$) representing the number of websites Bajtosia needs to visit. The $i$-th of the following lines (for $1 \le i \le n$) contains a non-empty string $s_i$ consisting of lowercase English letters (a-z), which is the address of the $i$-th website. The strings $s_i$ are pairwise distinct. Denoting $S = |s_1| + \dots + |s_n|$, we have $S \le 10^6$.

Output

In the first line of the output, print a single integer $k$ representing the minimum number of keystrokes required to visit all the given websites. The second line of the output should contain a sequence of $k$ keys that must be pressed in order to visit all the given websites. The BACKSPACE, ENTER, and TAB keys should be denoted by the letters B, E, and T, respectively. If there are multiple possible solutions, any of them may be printed.

Examples

Input 1

3
aaaaba
aaaaczzz
aaaadb

Output 1

21
aaaabaETBBdbETBBczzzE

Note 1

Explanation of the example: Bajtosia initially types the string aaaaba into the text field letter by letter, and then presses ENTER. Next, she presses the TAB key, after which the string aaaaba appears in the text field again. Then, Bajtosia presses BACKSPACE twice, obtaining aaaa, and appends two letters, obtaining aaaadb, after which she presses ENTER. Next, she presses the TAB key and BACKSPACE twice, again obtaining aaaa. Finally, she appends four letters and obtains aaaaczzz, after which she presses ENTER for the last time.

The entire process is also presented in the diagram in the section "Example solution diagram".

Sample tests

Test 0a is the example test above. Additionally:

0b: $n = 26$ strings, each consisting of 20,000 characters. The first 19,999 characters of each string are 'a', and their last characters are consecutive letters of the English alphabet ('a', 'b', ..., 'z').

0c: $n = 10$ strings, where the $i$-th one (for $i = 1, \dots, 10$) consists of the letter 'a' repeated $18i$ times.

0d: $n = 65,534$ strings. The input contains all non-empty strings consisting of letters 'a' and 'b' with a length no greater than 15.

0e: $n = 2$ strings. Both strings have a length of 500,000 and have the same letters on the first 300,000 positions. The characters at position 300,001 in both strings differ. Subsequent characters are random.

Example solution diagram

The diagram shows the consecutive steps of the solution for the example test described above.

Subtasks

Subtask Constraints Points
1 $n \le 8$ and $ s_i \le 10$ for $i \in [1..n]$ 17
2 all $s_i$ have the same length 12
3 $S \le 1\,000$ 32
4 $s_i$ consist only of letters a and b 18
5 no additional constraints 21

If only the first line of your answer is correct, your solution will receive 80% of the points for that test. You do not need to print the second line to receive these points.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.