E. Games on a CD

Codeforces
IDCF727E
Time4000ms
Memory512MB
Difficulty
data structureshashingstring suffix structuresstrings
English · Original
Chinese · Translation
Formal · Original
Several years ago Tolya had _n_ computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD **in clockwise order**. The names were distinct, the length of each name was equal to _k_. The names didn't overlap. Thus, there is a cyclic string of length _n_·_k_ written on the CD. Several years have passed and now Tolya can't remember which games he burned to his CD. He knows that there were _g_ popular games that days. All of the games he burned were among these _g_ games, and **no game was burned more than once**. You have to restore any valid list of games Tolya could burn to the CD several years ago. ## Input The first line of the input contains two positive integers _n_ and _k_ (1 ≤ _n_ ≤ 105, 1 ≤ _k_ ≤ 105) — the amount of games Tolya burned to the CD, and the length of each of the names. The second line of the input contains one string consisting of lowercase English letters — the string Tolya wrote on the CD, split in arbitrary place. The length of the string is _n_·_k_. It is guaranteed that the length is not greater than 106. The third line of the input contains one positive integer _g_ (_n_ ≤ _g_ ≤ 105) — the amount of popular games that could be written on the CD. It is guaranteed that the total length of names of all popular games is not greater than 2·106. Each of the next _g_ lines contains a single string — the name of some popular game. Each name consists of lowercase English letters and has length _k_. It is guaranteed that the names are distinct. ## Output If there is no answer, print "_NO_" (without quotes). Otherwise, print two lines. In the first line print "_YES_" (without quotes). In the second line, print _n_ integers — the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, **in clockwise order**. You can print games starting from any position. Remember, that no game was burned to the CD more than once. If there are several possible answers, print any of them. [samples]
几年前,Tolya 拥有 $n$ 个电脑游戏,并在某个时刻决定将它们刻录到 CD 上。之后,他将这些游戏的名称按顺时针顺序依次写在 CD 上,形成一个环形字符串。这些名称互不相同,每个名称的长度均为 $k$,且名称之间没有重叠。 因此,CD 上写有一个长度为 $n\cdot k$ 的循环字符串。 多年过去,Tolya 已经不记得他刻录了哪些游戏。但他记得当时有 $g$ 个流行的游戏。他刻录的所有游戏都来自这 $g$ 个游戏,且每个游戏至多被刻录一次。 你需要恢复出一个可能的、Tolya 当年刻录到 CD 上的游戏列表。 输入的第一行包含两个正整数 $n$ 和 $k$($1 ≤ n ≤ 10^5$, $1 ≤ k ≤ 10^5$)——分别表示 Tolya 刻录到 CD 上的游戏数量和每个游戏名称的长度。 输入的第二行包含一个由小写英文字母组成的字符串——这是 Tolya 写在 CD 上的字符串,但被任意切开成一条直线。该字符串长度为 $n\cdot k$,且保证其长度不超过 $10^6$。 输入的第三行包含一个正整数 $g$($n ≤ g ≤ 10^5$)——表示可能出现在 CD 上的流行游戏数量。保证所有流行游戏名称的总长度不超过 $2\cdot 10^6$。 接下来的 $g$ 行,每行包含一个字符串——某个流行游戏的名称。每个名称由小写英文字母组成,长度为 $k$,且保证所有名称互不相同。 如果没有解,请输出 "_NO_"(不含引号)。 否则,请输出两行。第一行输出 "_YES_"(不含引号)。第二行输出 $n$ 个整数——表示写在 CD 上的游戏(按它们在 CD 上的顺时针顺序)。你可以从任意位置开始输出,但必须保持顺时针顺序。记住,每个游戏至多被刻录一次。如果有多个可能的答案,输出任意一个即可。 ## Input 输入的第一行包含两个正整数 $n$ 和 $k$($1 ≤ n ≤ 10^5$, $1 ≤ k ≤ 10^5$)——分别表示 Tolya 刻录到 CD 上的游戏数量和每个游戏名称的长度。输入的第二行包含一个由小写英文字母组成的字符串——这是 Tolya 写在 CD 上的字符串,但被任意切开成一条直线。该字符串长度为 $n\cdot k$,且保证其长度不超过 $10^6$。输入的第三行包含一个正整数 $g$($n ≤ g ≤ 10^5$)——表示可能出现在 CD 上的流行游戏数量。保证所有流行游戏名称的总长度不超过 $2\cdot 10^6$。接下来的 $g$ 行,每行包含一个字符串——某个流行游戏的名称。每个名称由小写英文字母组成,长度为 $k$,且保证所有名称互不相同。 ## Output 如果没有解,请输出 "_NO_"(不含引号)。否则,请输出两行。第一行输出 "_YES_"(不含引号)。第二行输出 $n$ 个整数——表示写在 CD 上的游戏(按它们在 CD 上的顺时针顺序)。你可以从任意位置开始输出,但必须保持顺时针顺序。记住,每个游戏至多被刻录一次。如果有多个可能的答案,输出任意一个即可。 [samples]
**Definitions:** - Let $ n, k \in \mathbb{Z}^+ $: number of games and length of each game name. - Let $ S \in \Sigma^{nk} $: a cyclic string of length $ nk $, representing the concatenation of $ n $ distinct game names written in clockwise order on a CD, but cut at an arbitrary position. - Let $ G = \{g_1, g_2, \dots, g_g\} $: a set of $ g $ distinct game names, each of length $ k $, from which the burned games were chosen. - Each $ g_i \in G $ satisfies $ |g_i| = k $, and $ g_i \ne g_j $ for $ i \ne j $. **Constraints:** - The string $ S $ is a cyclic rotation of the concatenation $ T = t_1 t_2 \dots t_n $, where each $ t_i \in G $, and all $ t_i $ are distinct. - The goal is to find a sequence $ (t_1, t_2, \dots, t_n) \in G^n $, with all $ t_i $ distinct, such that $ S $ is a contiguous substring of length $ nk $ in the infinite repetition of $ T $, i.e., $ S \in \text{rotations}(T) $. **Objective:** Find any sequence $ (t_1, t_2, \dots, t_n) $ of $ n $ distinct elements from $ G $ such that the cyclic string $ t_1 t_2 \dots t_n $, when rotated appropriately, equals $ S $. Equivalently: - Let $ T = t_1 t_2 \dots t_n \in G^n $, all $ t_i $ distinct. - Then $ S $ must be a substring of length $ nk $ of the string $ T + T $ (i.e., $ T $ concatenated with itself). - We must recover such a sequence $ T $, or determine that none exists. **Formal Problem Statement:** Given: - $ n, k \in \mathbb{Z}^+ $ - $ S \in \Sigma^{nk} $ - $ G = \{g_1, \dots, g_g\} \subseteq \Sigma^k $, $ |G| = g \ge n $, all strings in $ G $ distinct and of length $ k $ Find: - A sequence $ (t_1, t_2, \dots, t_n) \in G^n $, with $ t_i \ne t_j $ for $ i \ne j $, such that there exists an integer $ r \in [0, nk - 1] $ satisfying: $$ S = (t_1 t_2 \dots t_n)[r : r + nk] \mod nk $$ where indexing is cyclic (i.e., $ S $ is a contiguous substring of length $ nk $ in the circular string $ t_1 t_2 \dots t_n $). If no such sequence exists, output "_NO_". Otherwise, output "_YES_" followed by the sequence $ t_1, t_2, \dots, t_n $ (in order around the circle, starting from any point).
Samples
Input #1
3 1
abc
4
b
a
c
d
Output #1
YES
2 1 3
Input #2
4 2
aabbccdd
4
dd
ab
bc
cd
Output #2
NO
API Response (JSON)
{
  "problem": {
    "name": "E. Games on a CD",
    "description": {
      "content": "Several years ago Tolya had _n_ computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD **in clo",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF727E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Several years ago Tolya had _n_ computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD **in clo...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "几年前,Tolya 拥有 $n$ 个电脑游戏,并在某个时刻决定将它们刻录到 CD 上。之后,他将这些游戏的名称按顺时针顺序依次写在 CD 上,形成一个环形字符串。这些名称互不相同,每个名称的长度均为 $k$,且名称之间没有重叠。\n\n因此,CD 上写有一个长度为 $n\\cdot k$ 的循环字符串。\n\n多年过去,Tolya 已经不记得他刻录了哪些游戏。但他记得当时有 $g$ 个流行的游戏。他刻录的所...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions:**\n\n- Let $ n, k \\in \\mathbb{Z}^+ $: number of games and length of each game name.\n- Let $ S \\in \\Sigma^{nk} $: a cyclic string of length $ nk $, representing the concatenation of $ n $ ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments