CF1714D.Color with Occurrences

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given some text tt and a set of nn strings s1,s2,…,sns_1, s_2, \dots, s_n.

In one step, you can choose any occurrence of any string sis_i in the text tt and color the corresponding characters of the text in red. For example, if t=bababat=\texttt{bababa} and s1=bas_1=\texttt{ba}, s2=abas_2=\texttt{aba}, you can get t=bababat=\color{red}{\texttt{ba}}\texttt{baba}, t=bababat=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba} or t=bababat=\texttt{bab}\color{red}{\texttt{aba}} in one step.

You want to color all the letters of the text tt in red. When you color a letter in red again, it stays red.

In the example above, three steps are enough:

  • Let's color t[2…4]=s2=abat[2 \dots 4]=s_2=\texttt{aba} in red, we get t=bababat=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba};
  • Let's color t[1…2]=s1=bat[1 \dots 2]=s_1=\texttt{ba} in red, we get t=bababat=\color{red}{\texttt{baba}}\texttt{ba};
  • Let's color t[4…6]=s2=abat[4 \dots 6]=s_2=\texttt{aba} in red, we get t=bababat=\color{red}{\texttt{bababa}}.

Each string sis_i can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.

Determine the minimum number of steps needed to color all letters tt in red and how to do it. If it is impossible to color all letters of the text tt in red, output -1.

给你一段文本 tt 和一个包含 nn 个字符串的集合 s1,s2,…,sns_1, s_2, \dots, s_n。

每一步中,你可以选择文本 tt 中任意一个字符串 sis_i 的任意一次出现,并将该出现所对应位置的字符染成红色。例如,若 t=bababat=\texttt{bababa},且 s1=bas_1=\texttt{ba}、s2=abas_2=\texttt{aba},则一步操作后可得到 t=bababat=\color{red}{\texttt{ba}}\texttt{baba}、t=bababat=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba} 或 t=bababat=\texttt{bab}\color{red}{\texttt{aba}}。

你的目标是将文本 tt 中的所有字母都染成红色。若某个字母已被染红,再次对其染色不会改变其颜色(即仍为红色)。

在上述例子中,三步操作即可完成全部染色:

  • 将 t[2…4]=s2=abat[2 \dots 4]=s_2=\texttt{aba} 染红,得到 t=bababat=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba};
  • 将 t[1…2]=s1=bat[1 \dots 2]=s_1=\texttt{ba} 染红,得到 t=bababat=\color{red}{\texttt{baba}}\texttt{ba};
  • 将 t[4…6]=s2=abat[4 \dots 6]=s_2=\texttt{aba} 染红,得到 t=bababat=\color{red}{\texttt{bababa}}。

每个字符串 sis_i 可以被使用任意多次(也可以完全不使用)。用于染色的字符串出现位置可以任意重叠。

请确定将文本 tt 中所有字母染成红色所需的最少步数,以及一种具体的染色方案。如果无法将文本 tt 中所有字母都染成红色,请输出 -1。

输入格式

The first line of the input contains an integer qq (1≤q≤1001 \le q \le 100) —the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains the text tt (1≤∣t∣≤1001 \le |t| \le 100), consisting only of lowercase Latin letters, where ∣t∣|t| is the length of the text tt.

The second line of each test case contains a single integer nn (1≤n≤101 \le n \le 10) — the number of strings in the set.

This is followed by nn lines, each containing a string sis_i (1≤∣si∣≤101 \le |s_i| \le 10) consisting only of lowercase Latin letters, where ∣si∣|s_i| — the length of string sis_i.

输入的第一行包含一个整数 qq(1≤q≤1001 \le q \le 100),表示测试用例的数量。

随后是各测试用例的描述。

每个测试用例的第一行包含文本 tt(1≤∣t∣≤1001 \le |t| \le 100),该文本仅由小写拉丁字母组成,其中 ∣t∣|t| 表示文本 tt 的长度。

每个测试用例的第二行包含一个整数 nn(1≤n≤101 \le n \le 10),表示集合中字符串的数量。

接下来是 nn 行,每行包含一个字符串 sis_i(1≤∣si∣≤101 \le |s_i| \le 10),该字符串仅由小写拉丁字母组成,其中 ∣si∣|s_i| 表示字符串 sis_i 的长度。

输出格式

For each test case, print the answer on a separate line.

If it is impossible to color all the letters of the text in red, print a single line containing the number -1.

Otherwise, on the first line, print the number mm — the minimum number of steps it will take to turn all the letters tt red.

Then in the next mm lines print pairs of indices: wjw_j and pjp_j (1≤j≤m1 \le j \le m), which denote that the string with index wjw_j was used as a substring to cover the occurrences starting in the text tt from position pjp_j. The pairs can be output in any order.

If there are several answers, output any of them.

对于每个测试用例,请在单独的一行上输出答案。

如果无法将文本中所有字母染成红色,则输出仅包含数字 −1-1 的一行。

否则,在第一行输出数字 mm —— 即将文本 tt 中所有字母变为红色所需的最少步数。

随后的 mm 行中,每行输出一对下标:wjw_j 和 pjp_j(1≤j≤m1 \le j \le m),表示使用了下标为 wjw_j 的字符串作为子串,来覆盖文本 tt 中从位置 pjp_j 开始的匹配。这些对可以以任意顺序输出。

若存在多个正确答案,输出其中任意一个即可。

输入输出样例

  • 输入#1

    6
    bababa
    2
    ba
    aba
    caba
    2
    bac
    acab
    abacabaca
    3
    aba
    bac
    aca
    baca
    3
    a
    c
    b
    codeforces
    4
    def
    code
    efo
    forces
    aaaabbbbcccceeee
    4
    eeee
    cccc
    aaaa
    bbbb

    输出#1

    3
    2 2
    1 1
    2 4
    -1
    4
    1 1
    2 6
    3 3
    3 7
    4
    3 1
    1 2
    2 3
    1 4
    2
    4 5
    2 1
    4
    3 1
    4 5
    2 9
    1 13

说明/提示

The first test case is explained in the problem statement.

In the second test case, it is impossible to color all the letters of the text in red.

第一个测试用例在题目描述中已作解释。

在第二个测试用例中,无法将文本中的所有字母都染成红色。

输入解题思路,AI测评打分。不知道怎么写?

首页