#abc344d. D - String Bags
D - String Bags
Score: points
问题描述
你最初有一个空字符串 。
另外,有 个袋子,每个袋子里包含一些字符串。
第 个袋子包含 个字符串 。
你将对 重复以下步骤:
- 选择并执行以下两种操作之一:
- 支付 日元,从第 个袋子中精确选择一个字符串,并将其拼接到 的末尾。
- 什么也不做。
给定一个字符串 ,找出使最终的 等于 所需的最小金额。
如果无法使最终的 等于 ,则输出 -1
。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You initially have an empty string .
Additionally, there are bags , each containing some strings.
Bag contains strings .
You will repeat the following steps for :
- Choose and perform one of the following two actions:
- Pay yen, select exactly one string from bag , and concatenate it to the end of .
- Do nothing.
Given a string , find the minimum amount of money required to make the final equal .
If there is no way to make the final equal , print -1
.
Constraints
- is a string consisting of lowercase English letters with length between and , inclusive.
- is an integer between and , inclusive.
- is an integer between and , inclusive.
- is a string consisting of lowercase English letters with length between and , inclusive.
Input
The input is given from Standard Input in the following format:
Output
Print the answer as an integer.
Sample Input 1
abcde
3
3 ab abc abcd
4 f c cd bcde
2 e de
Sample Output 1
2
For example, doing the following makes the final equal with two yen, which can be shown to be the minimum amount required.
- For , select
abc
from bag and concatenate it to the end of , makingabc
. - For , do nothing.
- For , select
de
from bag and concatenate it to the end of , makingabcde
.
Sample Input 2
abcde
3
2 ab abc
3 f c bcde
1 e
Sample Output 2
-1
There is no way to make the final equal , so print -1
.
Sample Input 3
aaabbbbcccc
6
2 aa aaa
2 dd ddd
2 ab aabb
4 bbaa bbbc bbb bbcc
2 cc bcc
3 ccc cccc ccccc
Sample Output 3
4