eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Rotate and Rewrite

Rotate and Rewrite

Two sequences of integers \textbf{A}: \textbf{A_1} \textbf{A_2} ... \textbf{A_n} and \textbf{B}: \textbf{B_1} \textbf{B_2} ... \textbf{B_m} and a set of rewriting rules of the form "\textbf{x_1} \textbf{x_2} ... \textbf{x_k} → \textbf{y}" are given. The following transformations on each of the sequences are allowed an arbitrary number of times in an arbitrary order independently. \begin{itemize} \item \textbf{Rotate}: Moving the first element of a sequence to the last. That is, transforming a sequence \textbf{c_1} \textbf{c_2} ... \textbf{c_p} to \textbf{c_2} ...\textbf{c_p} \textbf{c_1}. \item \textbf{Rewrite}: With a rewriting rule "\textbf{x_1} \textbf{x_2} ... \textbf{x_k} → \textbf{y}", transforming a sequence \textbf{c_1} \textbf{c_2} ... \textbf{c_i} \textbf{x_1} \textbf{x_2} ... \textbf{x_k} \textbf{d_1} \textbf{d_2} ... \textbf{d_j} to \textbf{c_\{1 \}c_2} ... \textbf{c_i} \textbf{y} \textbf{d_1} \textbf{d_2} ... \textbf{d_j}. \end{itemize} Your task is to determine whether it is possible to transform the two sequences \textbf{A} and \textbf{B} into the same sequence. If possible, compute the length of the longest of the sequences after such a transformation. \InputFile The input consists of multiple datasets. Each dataset has the following form. \textbf{n m r} \textbf{A_1 A_2 ... A_n} \textbf{B_1 B_2 ... B_m} \textbf{R_1} \textbf{...} \textbf{R_r} The first line of a dataset consists of three positive integers \textbf{n}, \textbf{m}, and \textbf{r}, where \textbf{n} (\textbf{n} ≤ \textbf{25}) is the length of the sequence \textbf{A}, \textbf{m} (\textbf{m} ≤ \textbf{25}) is the length of the sequence \textbf{B}, and \textbf{r} (\textbf{r} ≤ \textbf{60}) is the number of rewriting rules. The second line contains \textbf{n} integers representing the \textbf{n} elements of \textbf{A}. The third line contains \textbf{m} integers representing the m elements of \textbf{B}. Each of the last \textbf{r} lines describes a rewriting rule in the following form. \textbf{k x_1 x_2 ... x_k y} The first \textit{k} is an integer (\textbf{2} ≤ \textbf{k} ≤ \textbf{10}), which is the length of the left-hand side of the rule. It is followed by \textbf{k }integers \textbf{x_1} \textbf{x_2} ... \textbf{x_k}, representing the left-hand side of the rule. Finally comes an integer \textbf{y}, representing the right-hand side. All of \textbf{A_1}, .., \textbf{A_n}, \textbf{B_1}, ..., \textbf{B_m}, \textbf{x_1}, ..., \textbf{x_k}, and \textbf{y} are in the range between \textbf{1} and \textbf{30}, inclusive. A line "\textbf{0 0 0}" denotes the end of the input. \OutputFile For each dataset, if it is possible to transform \textbf{A} and \textbf{B} to the same sequence, print the length of the longest of the sequences after such a transformation. Print \textbf{-1} if it is impossible.
Лимит времени 8 секунд
Лимит использования памяти 64 MiB
Входные данные #1
3 3 3
1 2 3
4 5 6
2 1 2 5
2 6 4 3
2 5 3 1
3 3 2
1 1 1
2 2 1
2 1 1 2
2 2 2 1
7 1 2
1 1 2 1 4 1 2
4
3 1 4 1 4
3 2 4 2 4
16 14 5
2 1 2 2 1 3 2 1 3 2 2 1 1 3 1 2
2 1 3 1 1 2 3 1 2 2 2 2 1 3
2 3 1 3
3 2 2 2 1
3 2 2 1 2
3 1 2 2 2
4 2 1 2 2 2
0 0 0
Выходные данные #1
2
-1
1
9
Источник ACM International Collegiate Programming Contest, Japan Domestic Contest, Aizu, Japan, 2013-07-12