103443: [Atcoder]ABC344 D - String Bags

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:9 Solved:0

Description

Score: $425$ points

Problem Statement

You initially have an empty string $S$.
Additionally, there are bags $1, 2, \dots, N$, each containing some strings.
Bag $i$ contains $A_i$ strings $S_{i,1}, S_{i,2}, \dots, S_{i,A_i}$.

You will repeat the following steps for $i = 1, 2, \dots, N$:

  • Choose and perform one of the following two actions:
    • Pay $1$ yen, select exactly one string from bag $i$, and concatenate it to the end of $S$.
    • Do nothing.

Given a string $T$, find the minimum amount of money required to make the final $S$ equal $T$.
If there is no way to make the final $S$ equal $T$, print -1.

Constraints

  • $T$ is a string consisting of lowercase English letters with length between $1$ and $100$, inclusive.
  • $N$ is an integer between $1$ and $100$, inclusive.
  • $A_i$ is an integer between $1$ and $10$, inclusive.
  • $S_{i,j}$ is a string consisting of lowercase English letters with length between $1$ and $10$, inclusive.

Input

The input is given from Standard Input in the following format:

$T$
$N$
$A_1$ $S_{1,1}$ $S_{1,2}$ $\dots$ $S_{1,A_1}$
$A_2$ $S_{2,1}$ $S_{2,2}$ $\dots$ $S_{2,A_2}$
$\vdots$
$A_N$ $S_{N,1}$ $S_{N,2}$ $\dots$ $S_{N,A_N}$

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 $S$ equal $T$ with two yen, which can be shown to be the minimum amount required.

  • For $i=1$, select abc from bag $1$ and concatenate it to the end of $S$, making $S=$ abc.
  • For $i=2$, do nothing.
  • For $i=3$, select de from bag $3$ and concatenate it to the end of $S$, making $S=$ abcde.

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 $S$ equal $T$, 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

Input

Output

得分:425分 部分 问题描述 您最初有一个空字符串 S 。 此外,还有袋子 1, 2, ..., N,每个袋子都包含一些字符串。 袋子 i 包含 A_i 个字符串 S_{i,1}, S_{i,2}, ..., S_{i,A_i} 。 您将按照 i = 1, 2, ..., N 的顺序重复以下步骤: 选择并执行以下两项操作之一: * 支付 1 日元,从袋子 i 中选择一个字符串,并将其附加到 S 的末尾。 * 什么也不做。 给定一个字符串 T,找到使最终 S 等于 T 所需的最少金额。如果无法使最终 S 等于 T,则打印 -1 。 部分 约束 * T 是一个长度在 1 到 100 之间的由小写英文字母组成的字符串。 * N 是一个在 1 到 100 之间的整数。 * A_i 是一个在 1 到 10 之间的整数。 * S_{i,j} 是一个长度在 1 到 10 之间的由小写英文字母组成的字符串。 输入输出格式 输入 输入格式如下: T N A_1 S_{1,1} S_{1,2} \dots S_{1,A_1} A_2 S_{2,1} S_{2,2} \dots S_{2,A_2} \vdots A_N S_{N,1} S_{N,2} \dots S_{N,A_N} 输出 打印一个整数作为答案。 部分 示例输入 1 abcde 3 3 ab abc abcd 4 f c cd bcde 2 e de 部分 示例输出 1 2 例如,执行以下操作可以使最终 S 等于 T,所需金额为 2 日元,这可以证明是最少金额。 * 对于 i=1 ,从袋子 1 中选择 abc 并将其附加到 S 的末尾,使 S=abc 。 * 对于 i=2 ,什么也不做。 * 对于 i=3 ,从袋子 3 中选择 de 并将其附加到 S 的末尾,使 S=abcde 。 部分 示例输入 2 abcde 3 2 ab abc 3 f c bcde 1 e 部分 示例输出 2 -1 无法使最终 S 等于 T,因此打印 -1 。 部分 示例输入 3 aaabbbbcccc 6 2 aa aaa 2 dd ddd 2 ab aabb 4 bbaa bbbc bbb bbcc 2 cc bcc 3 ccc cccc ccccc 部分 示例输出 3 4

加入题单

算法标签: