#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 NN such routes, denoted by strings s1,s2,⋯ ,sNs_1, s_2, \cdots, s_N. We define the length of a route as the length of its string minus 11; for example, the route aqua has length 33.

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 tt to represent all destinations participating in the stamp-collecting activity. When a passenger collects all stamps corresponding to all letters in tt, 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 sis_i, starting from the starting point of sis_i, take sis_i to reach the ending point of sis_i, then take several more routes (possibly 00 routes) to finish collecting all stamps and return to the starting point of sis_i, 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 NN and a string tt, with 1≤N≤1051 \le N \le 10^5, 1≤∣t∣≤101 \le |t| \le 10, and tt containing only distinct lowercase letters.

The next NN lines each contain a string sis_i, representing the ii-th route. It is guaranteed that 2≤∣si∣≤1062 \le |s_i| \le 10^6, and ∑i=1N∣si∣≤106\sum_{i=1}^{N} |s_i| \le 10^6.

Output Format

Write to standard output.

Output NN 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 10%10\% of the data, it is guaranteed that 1≤N≤101 \le N \le 10, 1≤∣t∣≤51 \le |t| \le 5.
  • For another 10%10\% of the data, it is guaranteed that 1≤N≤10001 \le N \le 1000, ∣t∣=1|t| = 1.
  • For another 20%20\% of the data, it is guaranteed that 1≤∣t∣≤51 \le |t| \le 5.
  • For 100%100\% of the data, it is guaranteed that 1≤N≤1051 \le N \le 10^5, 1≤∣t∣≤101 \le |t| \le 10, 2≤∣si∣≤1062 \le |s_i| \le 10^6, ∑i=1N∣si∣≤106\sum_{i=1}^{N} |s_i| \le 10^6, sis_i and tt contain only lowercase letters, and the letters in tt are distinct.

Translated by ChatGPT 5