#P1132. 数字生成游戏

数字生成游戏

Description

Xiaoming has completed a number generation game. For a number ss that does not contain the digit 00, there are the following 33 rules to generate new numbers:

  1. Swap any two digits of ss to form a new number. For example, 143143 can generate 341,413,134341, 413, 134.

  2. Delete any single digit of ss to form a new number. For example, 143143 can generate 14,13,4314, 13, 43.

  3. Insert a digit xx between two adjacent digits si,si+1s_i, s_{i + 1} of ss, where xx must satisfy si<x<si+1s_i < x < s_{i + 1}. For example, 143143 can generate 1243,13431243, 1343, but cannot generate 1143,15431143, 1543, etc.

Now Xiaoming wants to know, under these rules, starting from ss, each step generating a new number (and then using the newly generated number to generate the next one), what is the minimum number of operations required to obtain tt.

Additionally, Xiaoming imposes a further restriction on rule 33: the number of digits of any generated number cannot exceed the number of digits of the initial number ss. If ss is 143143, then 12431243 and 13431343 are impossible to generate. If ss is 14431443, then you can first delete a digit to obtain 143143, and then generate 12431243 or 13431343.

Input Format

The first line contains 11 positive integer, the initial number ss.

The second line contains a positive integer mm, the number of queries.

The next mm lines each contain an integer tt (tt does not contain the digit 00), asking for the minimum number of operations needed to generate tt starting from ss. Any two queries are independent; numbers generated in one query do not carry over to the next, and only the initial number ss remains.

Output Format

Output mm lines. For each query, output one positive integer: the minimum number of operations. If it is impossible to transform under the rules, output 1-1.

143
3
134
133
32

1
-1
4

Hint

Sample explanation:

143134143 \to 134

133133 cannot be obtained.

143131232332143 \to 13 \to 123 \to 23 \to 32

Constraints

For 20%20\% of the testdata, s<100s < 100. For 40%40\% of the testdata, s<1000s < 1000. For 40%40\% of the testdata, m<10m < 10. For 60%60\% of the testdata, s<10000s < 10000. For 100%100\% of the testdata, s<100000,m50000s < 100000, m \leq 50000.

Translated by ChatGPT 5