#P16671. [CSPro 30] 闪耀巡航
[CSPro 30] 闪耀巡航
Background
Luogu’s testdata is only for non-official community use and is not official testdata. Official judging link: https://www.cspro.org/.
Problem Description
Xixi Aifu Island Travel Company has recently launched a series of shining cruise routes around Xixi Aifu Island. Ordinary routes are usually loops, so that passengers can return to the starting point after a long journey; while shining routes are mostly one-way routes with some thrills but no real danger. Therefore, Xixi Aifu Island Travel Company also allows passengers to choose combinations of routes that can return to the starting point.
Specifically, each route provided by the company can be represented by a string containing only lowercase letters, where each letter represents a destination visited along the route. For example, the route aqua represents a route that starts from a, passes through q and u, and finally returns to a. The company currently operates such routes, denoted by strings . We define the length of a route as the length of its string minus ; for example, the route aqua has length .
To encourage passengers to take its cruises, the company has launched a stamp-collecting activity. When taking a cruise and passing through certain destinations (which can be the starting point or the ending point of the taken route), passengers can obtain a stamp. We use a string to represent all destinations participating in the stamp-collecting activity. When a passenger collects all stamps corresponding to all letters in , they have a chance to win prizes such as free stays in luxury hotels.
To determine the expected profit brought by the stamp-collecting activity, the company wants to know: for each route , starting from the starting point of , take to reach the ending point of , then take several more routes (possibly routes) to finish collecting all stamps and return to the starting point of , what is the minimum total length of the route combination that needs to be taken.
Input Format
Read from standard input.
The first line contains a positive integer and a string , with , , and containing only distinct lowercase letters.
The next lines each contain a string , representing the -th route. It is guaranteed that , and .
Output Format
Write to standard output.
Output lines. Each line outputs a positive integer representing the minimum total length of the corresponding route combination, or output -1 if no route combination satisfies the requirement.
6 au
aqua
glass
hug
shiny
sparkling
youth
3
14
14
14
26
14
7 i
nonstop
perfect
rocket
thrilling
train
trapper
tripper
16
16
11
-1
16
22
11
Hint
Subtasks
- For of the data, it is guaranteed that , .
- For another of the data, it is guaranteed that , .
- For another of the data, it is guaranteed that .
- For of the data, it is guaranteed that , , , , and contain only lowercase letters, and the letters in are distinct.
Translated by ChatGPT 5