#P3294. [SCOI2016] 背单词

    ID: 2343 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心2016四川深度优先搜索,DFS字典树,Trie 树

[SCOI2016] 背单词

Description

Lweb is facing a mountain of English words and falls into deep thought: how can I finish learning quickly and then go play Sanguosha (pinyin)? At this moment, the wise Teacher Feng drifts over from afar. He gives Lweb a planner and a big jar of pickled chili peppers. His planner looks like this:

Index Word
11
22
\dots
n1n-1
nn

Then Teacher Feng tells Lweb: I know there are nn words you need to learn. We will fill the planner from top to bottom. For the word with index xx (the positions 1,,x11,\dots ,x-1 have already been filled):

  1. If there exists a word that is its suffix and has not yet been filled into the table, then he needs to eat n×nn \times n chili peppers to learn it.
  2. If all of its suffixes have already been filled into the table, and none of the words at positions 1,,x11,\dots ,x-1 is its suffix, then you can remember it by eating xx chili peppers.
  3. If all of its suffixes have already been filled into the table, and among positions 1,,x11,\dots ,x-1 there exists a word that is its suffix, let the maximum index among its suffixes be yy. Then you only need to eat xyx-y chili peppers to remember it.

Lweb is a strange kid who goes berserk when he eats spicy things, so please help Lweb find an optimal way to fill the words so that he learns these nn words while eating the fewest chili peppers.

Formal statement:

You need to arrange an order for nn strings. Each string incurs a certain cost.

For a string ss at position xx:

  1. If there exists at least one other string that is a suffix of ss, and that string’s position is after ss, then ss incurs a cost of n×nn \times n.
  2. If no other string is a suffix of ss, then ss incurs a cost of xx.
  3. If all strings that are suffixes of ss appear before ss, and the maximum position among them is yy, then ss incurs a cost of xyx-y.

Arrange an order for the nn strings to minimize the total cost.

Input Format

Input an integer nn, the number of words Lweb needs to learn.

Then follow nn lines, each containing one word (lowercase letters only, and all words are pairwise distinct).

Output Format

The minimum number of chili peppers Lweb needs to eat.

2
a
ba
2

Hint

Constraints: 1n1000001\le n\le100000, and the total length of all strings satisfies 1S5100001\le \sum|S| \le510000.

Translated by ChatGPT 5