F - Shik and Copying String Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 15001500 points

Problem Statement

Shik's job is very boring. At day 00, his boss gives him a string S0S_0 of length NN which consists of only lowercase English letters. In the ii-th day after day 00, Shik's job is to copy the string Si1S_{i-1} to a string SiS_i. We denote the jj-th letter of SiS_i as Si[j]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, Si[j]S_i[j] is equal to either Si1[j]S_{i-1}[j] or Si[j1]S_{i}[j-1]. (Note that Si[1]S_i[1] always equals to Si1[1]S_{i-1}[1].)

You are given the string S0S_0 and another string TT. Please determine the smallest integer ii such that SiS_i could be equal to TT. If no such ii exists, please print -1.

Constraints

  • 1N1,000,0001 \leq N \leq 1,000,000
  • The lengths of S0S_0 and TT are both NN.
  • Both S0S_0 and TT consist of lowercase English letters.

Input

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

NN
S0S_0
TT

Output

Print the smallest integer ii such that SiS_i could be equal to TT. If no such ii exists, print -1 instead.


Sample Input 1Copy

Copy
5
abcde
aaacc

Sample Output 1Copy

Copy
2

S0S_0 = abcde, S1S_1 = aaccc and S2S_2 = aaacc is a possible sequence such that S2=TS_2 = T.


Sample Input 2Copy

Copy
5
abcde
abcde

Sample Output 2Copy

Copy
0

Sample Input 3Copy

Copy
4
acaa
aaca

Sample Output 3Copy

Copy
2

Sample Input 4Copy

Copy
5
abcde
bbbbb

Sample Output 4Copy

Copy
-1

配点 : 15001500

問題文

シックの仕事はコピー取りです。ある日、シックは上司から英小文字からなる長さ NN の文字列 S0S_0 を受け取りました(この日を 00 日目とします)。これ以降、ii 日目の仕事は、文字列 Si1S_{i-1}SiS_i にコピーすることです。以下、SiS_ijj 番目の文字を Si[j]S_i[j] と表します。

シックはまだこの仕事に慣れていません。毎日、文字列を先頭の文字から順に書き写していくのですが、正しい文字の代わりに誤って直前に書いた文字と同じ文字を書いてしまうことがあります。すなわち、Si[j]S_i[j]Si1[j]S_{i-1}[j] または Si[j1]S_{i}[j-1] のどちらかと等しくなります。(ただし、文字列の先頭の文字を書き間違えることはありません。すなわち、Si[1]S_i[1] は必ず Si1[1]S_{i-1}[1] と等しくなります。)

二つの文字列 S0S_0TT が与えられます。SiS_iTT と等しくなる可能性があるような最小の整数 ii を求めてください。もしそのような ii が存在しなければ、代わりに -1 と答えてください。

制約

  • 1N1,000,0001 \leq N \leq 1,000,000
  • S0S_0TT の長さはともに NN である。
  • S0S_0TT はともに英小文字のみからなる。

入力

入力は以下の形式で標準入力から与えられる。

NN
S0S_0
TT

出力

SiS_iTT と等しくなる可能性のあるような ii が存在するならば、そのような ii のうち最小のものを出力せよ。そのような ii が存在しなければ、代わりに -1 と出力せよ。


入力例 1Copy

Copy
5
abcde
aaacc

出力例 1Copy

Copy
2

S0S_0 = abcde, S1S_1 = aaccc, S2S_2 = aaacc のように、S2=TS_2 = T となる可能性が存在します。


入力例 2Copy

Copy
5
abcde
abcde

出力例 2Copy

Copy
0

入力例 3Copy

Copy
4
acaa
aaca

出力例 3Copy

Copy
2

入力例 4Copy

Copy
5
abcde
bbbbb

出力例 4Copy

Copy
-1


2025-03-15 (Sat)
03:20:33 +00:00