Problem:
Score : 1500 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

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.

Sample Input 1

5
abcde
aaacc

Sample Output 1

2

S_0 = abcde, S_1 = aaccc and S_2 = aaacc is a possible sequence such that S_2 = T.

Sample Input 2

5
abcde
abcde

Sample Output 2

0

Sample Input 3

4
acaa
aaca

Sample Output 3

2

Sample Input 4

5
abcde
bbbbb

Sample Output 4

-1

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
	return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
char S[1000005], T[1000005];
int main() {
	int n;
	scanf("%d%s%s", &n, S, T);
	if (string(S) == string(T)) {
		printf("0\n");
		return 0;
	}
	int las = n;
	vector<int> A = {n}, B;
	for (int i = n - 1; i >= 0; --i) {
		if (!i || T[i - 1] != T[i]) {
			B.push_back(i);
			bool found = false;
			for (int j = min(las - 1, i); j >= 0; --j) {
				if (S[j] == T[i]) {
					las = j;
					A.push_back(j);
					found = true;
					break;
				}
			}
			if (!found) {
				printf("-1\n");
				return 0;
			}
		}
	}
	assert(A.size() == B.size() + 1);
	reverse(A.begin(), A.end());
	reverse(B.begin(), B.end());
	int ans = 0;
	int m = A.size();
	for (int i = 0; i + 1 < m; ++i) {
		int lo = 0, hi = m - 1 - i;
		while (lo < hi) {
			int j = (lo + hi) >> 1;
			if (A[i + j] - j >= B[i]) hi = j;
			else lo = j + 1;
		}
		assert(A[i + lo] - lo >= B[i]);
		ans = max(ans, lo);
	}
	printf("%d\n", ans + 1);
}