C. Hidden Word

Codeforces
IDCF725C
Time2000ms
Memory256MB
Difficulty
brute forceconstructive algorithmsimplementationstrings
English · Original
Chinese · Translation
Formal · Original
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid: <center> ABCDEFGHIJKLM NOPQRSTUVWXYZ </center>We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter '_A_' is adjacent only to the tiles with letters '_B_', '_N_', and '_O_'. A tile is not adjacent to itself. A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "_ABC_" is a path, and so is "_KXWIHIJK_". "_MAB_" is not a path because '_M_' is not adjacent to '_A_'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself). You’re given a string _s_ which consists of 27 upper-case English letters. Each English letter **occurs at least once** in _s_. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string _s_. If there’s no solution, print "_Impossible_" (without the quotes). ## Input The only line of the input contains the string _s_, consisting of 27 upper-case English letters. Each English letter occurs at least once in _s_. ## Output Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "_Impossible_". [samples]
我们定义一个网格为一个包含 #cf_span[2] 行和 #cf_span[13] 列的方格阵列。每个格子中写有一个英文大写字母。字母不必唯一:可能存在两个或多个格子写有相同的字母。以下是一个网格示例: 我们称两个格子是相邻的,如果它们共享一条边或一个角。在上面的示例网格中,字母为 '_A_' 的格子仅与字母为 '_B_'、'_N_' 和 '_O_' 的格子相邻。一个格子不与自身相邻。 一个格子序列被称为一条路径,如果序列中的每个格子都与它后面的格子相邻(序列中最后一个格子没有后续格子)。在本例中,"_ABC_" 是一条路径,"_KXWIHIJK_" 也是。"_MAB_" 不是一条路径,因为 '_M_' 不与 '_A_' 相邻。一条路径可以多次使用同一个格子(但同一个格子不能在路径中连续出现两次,因为没有任何格子与自身相邻)。 给你一个字符串 #cf_span[s],它由 #cf_span[27] 个大写英文字母组成。每个英文字母在 #cf_span[s] 中至少出现一次。请找出一个网格,使得其中存在一条路径,其经过的格子按访问顺序形成的字符串恰好为 #cf_span[s]。如果无解,请输出 "_Impossible_"(不含引号)。 输入仅一行,包含一个由 #cf_span[27] 个大写英文字母组成的字符串 #cf_span[s]。每个英文字母在 #cf_span[s] 中至少出现一次。 请输出两行,每行包含 #cf_span[13] 个大写英文字母,表示网格的两行。如果存在多个解,输出任意一个即可。如果无解,请输出 "_Impossible_"。 ## Input 输入仅一行,包含一个由 #cf_span[27] 个大写英文字母组成的字符串 #cf_span[s]。每个英文字母在 #cf_span[s] 中至少出现一次。 ## Output 请输出两行,每行包含 #cf_span[13] 个大写英文字母,表示网格的两行。如果存在多个解,输出任意一个即可。如果无解,请输出 "_Impossible_"。 [samples]
**Definitions** Let $ s = s_1 s_2 \dots s_{27} $ be a string of 27 uppercase English letters, where each letter from 'A' to 'Z' appears at least once. Let $ G $ be a $ 2 \times 13 $ grid, i.e., a matrix with 2 rows and 13 columns, where each cell contains an uppercase English letter. **Constraints** 1. Each letter in $ \{A, B, \dots, Z\} $ appears at least once in $ s $. 2. A path in $ G $ is a sequence of positions $ (r_1, c_1), (r_2, c_2), \dots, (r_{27}, c_{27}) $ such that: - For all $ i \in \{1, \dots, 26\} $, the tile at $ (r_i, c_i) $ is adjacent (including diagonally) to the tile at $ (r_{i+1}, c_{i+1}) $. - $ (r_i, c_i) \neq (r_{i+1}, c_{i+1}) $ for all $ i $. - $ G[r_i][c_i] = s_i $ for all $ i \in \{1, \dots, 27\} $. **Objective** Find a grid $ G \in \{A, B, \dots, Z\}^{2 \times 13} $ that contains such a path, or output "Impossible" if no such grid exists.
Samples
Input #1
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output #1
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input #2
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output #2
Impossible
API Response (JSON)
{
  "problem": {
    "name": "C. Hidden Word",
    "description": {
      "content": "Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF725C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "我们定义一个网格为一个包含 #cf_span[2] 行和 #cf_span[13] 列的方格阵列。每个格子中写有一个英文大写字母。字母不必唯一:可能存在两个或多个格子写有相同的字母。以下是一个网格示例:\n\n我们称两个格子是相邻的,如果它们共享一条边或一个角。在上面的示例网格中,字母为 '_A_' 的格子仅与字母为 '_B_'、'_N_' 和 '_O_' 的格子相邻。一个格子不与自身相邻。\n\n一个格...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ s = s_1 s_2 \\dots s_{27} $ be a string of 27 uppercase English letters, where each letter from 'A' to 'Z' appears at least once.  \nLet $ G $ be a $ 2 \\times 13 $ grid, i.e., a ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments