#P3294. [SCOI2016] 背单词
[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 |
|---|---|
Then Teacher Feng tells Lweb: I know there are words you need to learn. We will fill the planner from top to bottom. For the word with index (the positions have already been filled):
- If there exists a word that is its suffix and has not yet been filled into the table, then he needs to eat chili peppers to learn it.
- If all of its suffixes have already been filled into the table, and none of the words at positions is its suffix, then you can remember it by eating chili peppers.
- If all of its suffixes have already been filled into the table, and among positions there exists a word that is its suffix, let the maximum index among its suffixes be . Then you only need to eat 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 words while eating the fewest chili peppers.
Formal statement:
You need to arrange an order for strings. Each string incurs a certain cost.
For a string at position :
- If there exists at least one other string that is a suffix of , and that string’s position is after , then incurs a cost of .
- If no other string is a suffix of , then incurs a cost of .
- If all strings that are suffixes of appear before , and the maximum position among them is , then incurs a cost of .
Arrange an order for the strings to minimize the total cost.
Input Format
Input an integer , the number of words Lweb needs to learn.
Then follow 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: , and the total length of all strings satisfies .
Translated by ChatGPT 5
京公网安备 11011102002149号