201541: [AtCoder]ARC154 B - New Place

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $400$ points

Problem Statement

You are given strings $S$ and $T$ of length $N$ consisting of lowercase English letters.

You can repeat the following operation any number of times (possibly zero).

  • Erase the first character of $S$ and insert the same character at any position of $S$.

Determine whether it is possible to make $S$ equal $T$, and if it is possible, find the minimum number of operations needed.

Constraints

  • $1 \le N \le 2 \times 10^5$
  • $S$ and $T$ are strings of length $N$ consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

$N$
$S$
$T$

Output

If it is impossible to make $S$ equal $T$, print -1. If it is possible, print the minimum number of operations needed.


Sample Input 1

4
abab
abba

Sample Output 1

2

You can make $S$ equal $T$ in two operations, as follows.

  • Erase the first character of $S$, and insert that character a at the end of $S$, making $S$ baba.
  • Erase the first character of $S$, and insert that character b between the $2$-nd and $3$-rd characters of $S$, making $S$ abba.

It is impossible to make $S$ equal $T$ in one or fewer operations, so the answer is $2$.


Sample Input 2

3
arc
cra

Sample Output 2

2

Input

题意翻译

### 题目描述 给你两个长度为 $N$ 的字符串 $S$ 和 $T$,仅包含英文小写字母。 你可以重复进行下面的操作(可以不执行): - 将 $S$ 的第一个字符删去,并将这个字符插入到 $S$ 的任意位置。 问你至少执行多少次操作使得 $S$ 与 $T$ 相等。 ### 输入格式 第一行是一个整数 $N$。第二行是字符串 $S$,第三行是字符串 $T$。 ### 输出格式 如果 $S$ 不可能与 $T$ 相等,输出 `-1`。 否则,输出使 $S$ 和 $T$ 相等所需的最小操作数。 @[hellolin](/user/751017) 译

加入题单

算法标签: