QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#12661. 打字机

Statistics

你需要使用活字印刷机打印 $N$ 个单词。活字印刷机是一种古老的打印机,需要你按顺序放置小金属块(每个金属块包含一个字母)来组成单词。然后将一张纸压在这些金属块上以打印出单词。你所使用的打印机允许你执行以下任一操作:

  • 在当前打印机中的单词末尾添加一个字母。
  • 移除当前打印机中单词的最后一个字母。仅当打印机中至少有一个字母时,才允许执行此操作。
  • 打印当前打印机中的单词。

初始时,打印机是空的;它不包含任何带有字母的金属块。在打印结束时,允许打印机中保留一些字母。此外,你可以按任意顺序打印这些单词。

由于每次操作都需要时间,你希望最小化总操作次数。

任务

你必须编写一个程序,给定你想要打印的 $N$ 个单词,找出打印所有单词所需的最少操作次数,并输出其中一种操作序列。

数据范围

$1 \le N \le 25,000$:你需要打印的单词数量。

输入格式

你的程序必须从标准输入读取以下数据:

  • 第 1 行包含整数 $N$,即你需要打印的单词数量。
  • 接下来的 $N$ 行,每行包含一个单词。每个单词仅由小写字母('a' – 'z')组成,长度在 1 到 20 之间(包含 1 和 20)。

所有单词各不相同。

输出格式

你的程序必须向标准输出写入以下数据:

  • 第 1 行必须包含一个整数 $M$,表示打印这 $N$ 个单词所需的最少操作次数。
  • 接下来的 $M$ 行,每行包含一个字符。这些字符描述了所执行的操作序列。每个操作描述如下:
    • 添加一个字母由该字母本身(小写)表示。
    • 移除最后一个字母由字符 '-'(减号,ASCII 码 45)表示。
    • 打印当前单词由字符 'P'(大写字母 P)表示。

子任务

对于部分测试点,总分值为 40 分,$N$ 不超过 18。

样例

样例输入 1

3
print
the
poem

样例输出 1

20
t
h
e
P
-
-
-
p
o
e
m
P
-
-
-
r
i
n
t
P

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.