luogu#P4683. [IOI 2008] Type Printer

    ID: 3667 远端评测题 1000ms 63MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>字符串2008IOISpecial Judge深度优先搜索 DFS字典树 Trie

[IOI 2008] Type Printer

Problem Description

You need to use a movable printer to print nn words. This movable printer is an old-style printer: you have to place some small metal blocks (each containing one letter) onto the printer to form a word. Then you press these metal blocks onto a sheet of paper to print the word. The printer allows you to perform the following operations:

  • Add one letter to the end (tail) of the current word in the printer.
  • Delete one letter from the end of the current word (remove the last letter). This operation is allowed only when the current word has at least one letter.
  • Print the current word in the printer.

Initially, the printer is empty, i.e., it contains no metal blocks with letters. After finishing all printing, it is allowed to leave some letters in the printer. You are also allowed to print the words in any order.

Since each operation takes some time, you need to minimize the total number of operations.

You need to write a program that, given the nn words to be printed, finds the minimum number of operations needed to print all words in some order, and outputs one such sequence of operations.

Input Format

  • Line 11 contains an integer nn, the number of words you need to print.
  • The next nn lines each contain one word. Each word consists only of lowercase letters, and its length is from 11 to 2020 letters (inclusive). All words are distinct.

Output Format

The first line contains an integer mm, the minimum number of operations required to print these nn words.

The next mm lines each contain one character, describing your operation sequence as follows:

  • To add a letter, output that lowercase letter itself.
  • To delete a letter, output -.
  • To print the current word, output P.
3
print
the
poem
20
t
h
e
P
-
-
-
p
o
e
m
P
-
-
-
r
i
n
t
P

Hint

For 40%40\% of the testdata, n18n \leq 18.

For 100%100\% of the testdata, 1n250001 \leq n \leq 25000.

Translated by ChatGPT 5