#P1132. 数字生成游戏
数字生成游戏
Description
Xiaoming has completed a number generation game. For a number that does not contain the digit , there are the following rules to generate new numbers:
-
Swap any two digits of to form a new number. For example, can generate .
-
Delete any single digit of to form a new number. For example, can generate .
-
Insert a digit between two adjacent digits of , where must satisfy . For example, can generate , but cannot generate , etc.
Now Xiaoming wants to know, under these rules, starting from , 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 .
Additionally, Xiaoming imposes a further restriction on rule : the number of digits of any generated number cannot exceed the number of digits of the initial number . If is , then and are impossible to generate. If is , then you can first delete a digit to obtain , and then generate or .
Input Format
The first line contains positive integer, the initial number .
The second line contains a positive integer , the number of queries.
The next lines each contain an integer ( does not contain the digit ), asking for the minimum number of operations needed to generate starting from . Any two queries are independent; numbers generated in one query do not carry over to the next, and only the initial number remains.
Output Format
Output lines. For each query, output one positive integer: the minimum number of operations. If it is impossible to transform under the rules, output .
143
3
134
133
32
1
-1
4
Hint
Sample explanation:
cannot be obtained.
Constraints
For of the testdata, . For of the testdata, . For of the testdata, . For of the testdata, . For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号