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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL) INF;

const int MAX = 1001000;

char S[MAX];
char T[MAX];

int A[MAX];

int main()
{
	//freopen("in.txt", "r", stdin);
	//ios::sync_with_stdio(false); cin.tie(0);

	int n;
	scanf("%d", &n);
	scanf("%s", S);
	scanf("%s", T);

	if ((string)(S) ==(string(T)))
	{
		cout<<0<<endl;
		return 0;
	}

	FILL(A, -1);
	int ind = n-1;
	RFOR(i, n, 0)
	{
		while (ind > i) ind--;
		while(ind >= 0 && S[ind] != T[i])
		{
			ind--;
		}

		if (ind == -1)
		{
			cout<<-1<<endl;
			return 0;
		}

		if (A[ind] == -1) A[ind] = i;
	}

	vector<PII> a;
	FOR (i, 0, n)
	{
		if (A[i] != -1) a.PB(MP(i, A[i]));
	}

	/*FOR (i, 0, SZ(a))
	{
		cout<<a[i].first<<' '<<a[i].second<<endl;
	}
	cout<<endl;*/

	int res = 0;

	ind = 0;
	int ind2 = 0;
	FOR (i, 0, SZ(a))
	{
		while(true)
		{
			if (ind2 == SZ(a)) break;
			if (a[ind2].first <= a[i].second)
			{
				ind2++;
				continue;
			}
			break;
		}
		while(true)
		{
			if (ind == SZ(a)) break;
			if (a[ind].first - (ind - i - 1) <= a[i].second)
			{
				ind++;
				continue;
			}
			break;
		}

		res = max(res, ind - i);
	}

	cout<<res<<endl;

}