AtCoder Grand Contest 007
F
Shik and Copying String
AtCoder
⌀ 3642
原题
← E→
翻译中…

Problem Statement

翻译中…

Shik's job is very boring. At day $0$, his boss gives him a string $S_0$ of length $N$ which consists of only lowercase English letters. In the $i$-th day after day $0$, Shik's job is to copy the string $S_{i-1}$ to a string $S_i$. We denote the $j$-th letter of $S_i$ as $S_i[j]$.

翻译中…

Shik is inexperienced in this job. In each day, when he is copying letters one by one from the first letter to the last letter, he would make mistakes. That is, he sometimes accidentally writes down the same letter that he wrote previously instead of the correct one. More specifically, $S_i[j]$ is equal to either $S_{i-1}[j]$ or $S_{i}[j-1]$. (Note that $S_i[1]$ always equals to $S_{i-1}[1]$.)

翻译中…

You are given the string $S_0$ and another string $T$. Please determine the smallest integer $i$ such that $S_i$ could be equal to $T$. If no such $i$ exists, please print -1.

翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 1,000,000$
  • The lengths of $S_0$ and $T$ are both $N$.
  • Both $S_0$ and $T$ consist of lowercase English letters.
翻译中…

Input

翻译中…

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

翻译中…

N
S_0
T

翻译中…

Output

翻译中…

Print the smallest integer $i$ such that $S_i$ could be equal to $T$. If no such $i$ exists, print -1 instead.

翻译中…

样例

Input #1
5
abcde
aaacc
Output #1
2
$S_0$ = `abcde`, $S_1$ = `aaccc` and $S_2$ = `aaacc` is a possible sequence such that $S_2 = T$.
Input #2
5
abcde
abcde
Output #2
0
Input #3
4
acaa
aaca
Output #3
2
Input #4
5
abcde
bbbbb
Output #4
-1
题目信息
题号
agc007_f
时间限制
2 s
内存限制
256 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。