B. Obtaining the String

Codeforces
IDCF1015B
Time1000ms
Memory256MB
Difficulty
implementation
English · Original
Chinese · Translation
Formal · Original
You are given two strings $s$ and $t$. Both strings have length $n$ and consist of lowercase Latin letters. The characters in the strings are numbered from $1$ to $n$. You can successively perform the following move any number of times (possibly, zero): * swap any two adjacent (neighboring) characters of $s$ (i.e. for any $i = {1, 2, \dots, n - 1}$ you can swap $s_i$ and $s_{i + 1})$. You can't apply a move to the string $t$. The moves are applied to the string $s$ one after another. Your task is to obtain the string $t$ from the string $s$. Find any way to do it with at most $10^4$ such moves. _You do not have to minimize the number of moves, just find any sequence of moves of length $10^4$ or less to transform $s$ into $t$._ ## Input The first line of the input contains one integer $n$ ($1 \le n \le 50$) — the length of strings $s$ and $t$. The second line of the input contains the string $s$ consisting of $n$ lowercase Latin letters. The third line of the input contains the string $t$ consisting of $n$ lowercase Latin letters. ## Output If it is impossible to obtain the string $t$ using moves, print "_\-1_". Otherwise in the first line print one integer $k$ — the number of moves to transform $s$ to $t$. Note that $k$ must be an integer number between $0$ and $10^4$ inclusive. In the second line print $k$ integers $c_j$ ($1 \le c_j < n$), where $c_j$ means that on the $j$\-th move you swap characters $s_{c_j}$ and $s_{c_j + 1}$. If you do not need to apply any moves, print a single integer $0$ in the first line and either leave the second line empty or do not print it at all. [samples] ## Note In the first example the string $s$ changes as follows: "_abcdef_" $\rightarrow$ "_abdcef_" $\rightarrow$ "_abdcfe_" $\rightarrow$ "_abdfce_" $\rightarrow$ "_abdfec_". In the second example there is no way to transform the string $s$ into the string $t$ through any allowed moves.
你被给予两个字符串 $s$ 和 $t$。两个字符串长度均为 $n$,且均由小写拉丁字母组成。字符串中的字符编号从 $1$ 到 $n$。 你可以任意次数(可能为零次)依次执行以下操作: 你不能对字符串 $t$ 应用任何操作。所有操作均作用于字符串 $s$,并依次进行。 你的任务是通过这些操作将字符串 $s$ 变为字符串 $t$。请找出一种在不超过 $10^4$ 次操作内完成变换的方法。 _你无需最小化操作次数,只需找到任意一个长度不超过 $10^4$ 的操作序列,将 $s$ 转换为 $t$。_ 输入的第一行包含一个整数 $n$ ($1 lt.eq n lt.eq 50$) —— 字符串 $s$ 和 $t$ 的长度。 第二行包含字符串 $s$,由 $n$ 个小写拉丁字母组成。 第三行包含字符串 $t$,由 $n$ 个小写拉丁字母组成。 如果无法通过操作得到字符串 $t$,请输出 "_-1_"。 否则,在第一行输出一个整数 $k$ —— 将 $s$ 转换为 $t$ 所需的操作次数。注意 $k$ 必须是介于 $0$ 和 $10^4$ 之间(含端点)的整数。 在第二行输出 $k$ 个整数 $c_j$ ($1 lt.eq c_j < n$),其中 $c_j$ 表示第 $j$ 次操作中交换字符 $s_{c_j}$ 和 $s_{c_j + 1}$。 如果无需执行任何操作,请在第一行输出单个整数 $0$,第二行可留空或不输出。 在第一个示例中,字符串 $s$ 的变化过程如下:"_abcdef_" $arrow.r$ "_abdcef_" $arrow.r$ "_abdcfe_" $arrow.r$ "_abdfce_" $arrow.r$ "_abdfec_"。 在第二个示例中,无法通过任何允许的操作将字符串 $s$ 转换为字符串 $t$。 ## Input 输入的第一行包含一个整数 $n$ ($1 lt.eq n lt.eq 50$) —— 字符串 $s$ 和 $t$ 的长度。第二行包含字符串 $s$,由 $n$ 个小写拉丁字母组成。第三行包含字符串 $t$,由 $n$ 个小写拉丁字母组成。 ## Output 如果无法通过操作得到字符串 $t$,请输出 "_-1_"。否则,在第一行输出一个整数 $k$ —— 将 $s$ 转换为 $t$ 所需的操作次数。注意 $k$ 必须是介于 $0$ 和 $10^4$ 之间(含端点)的整数。在第二行输出 $k$ 个整数 $c_j$ ($1 lt.eq c_j < n$),其中 $c_j$ 表示第 $j$ 次操作中交换字符 $s_{c_j}$ 和 $s_{c_j + 1}$。如果无需执行任何操作,请在第一行输出单个整数 $0$,第二行可留空或不输出。 [samples] ## Note 在第一个示例中,字符串 $s$ 的变化过程如下:"_abcdef_" $arrow.r$ "_abdcef_" $arrow.r$ "_abdcfe_" $arrow.r$ "_abdfce_" $arrow.r$ "_abdfec_"。在第二个示例中,无法通过任何允许的操作将字符串 $s$ 转换为字符串 $t$。
**Definitions** Let $ n \in \mathbb{Z} $ be the length of strings $ s $ and $ t $. Let $ s = (s_1, s_2, \dots, s_n) $, $ t = (t_1, t_2, \dots, t_n) $ be strings over the alphabet of lowercase Latin letters. **Constraints** 1. $ 1 \le n \le 50 $ 2. Each character $ s_i, t_i \in \{ \text{a}, \text{b}, \dots, \text{z} \} $ **Allowed Operation** A move consists of selecting an index $ c \in \{1, 2, \dots, n-1\} $ and swapping $ s_c $ and $ s_{c+1} $. **Objective** Find a sequence of moves $ (c_1, c_2, \dots, c_k) $ with $ 0 \le k \le 10^4 $ such that applying these swaps in order to $ s $ yields $ t $. If no such sequence exists, output $-1$.
Samples
Input #1
6
abcdef
abdfec
Output #1
4
3 5 4 5
Input #2
4
abcd
accd
Output #2
\-1
API Response (JSON)
{
  "problem": {
    "name": "B. Obtaining the String",
    "description": {
      "content": "You are given two strings $s$ and $t$. Both strings have length $n$ and consist of lowercase Latin letters. The characters in the strings are numbered from $1$ to $n$. You can successively perform th",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1015B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given two strings $s$ and $t$. Both strings have length $n$ and consist of lowercase Latin letters. The characters in the strings are numbered from $1$ to $n$.\n\nYou can successively perform th...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给予两个字符串 $s$ 和 $t$。两个字符串长度均为 $n$,且均由小写拉丁字母组成。字符串中的字符编号从 $1$ 到 $n$。\n\n你可以任意次数(可能为零次)依次执行以下操作:\n\n你不能对字符串 $t$ 应用任何操作。所有操作均作用于字符串 $s$,并依次进行。\n\n你的任务是通过这些操作将字符串 $s$ 变为字符串 $t$。请找出一种在不超过 $10^4$ 次操作内完成变换的方法。\n\n_你...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the length of strings $ s $ and $ t $.  \nLet $ s = (s_1, s_2, \\dots, s_n) $, $ t = (t_1, t_2, \\dots, t_n) $ be strings over the alphabet of lowercase Lati...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments