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<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<cmath>
#include<string>

#define ls (t<<1)
#define rs ((t<<1)+1)
#define mid ((l+r)>>1)
#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 1000005
#define M 200005
#define seed 23333
#define Mo 998244353

using namespace std;
int i,j,m,n,p,k,ID[N],tot,Str[N],End[N],f[N],g[N],T[N];
int kong[N],vis[N],sum,full[N];
char c[N],title[N],s[N];
vector<pair<int,int> >ques[N];
void bl()
{
	int i,ans=0,j,k,last;
	for (;;)
	{
		++ans;
		last=n+1;
		for (i=n;i;--i)
			if (s[i]!=c[i])
			{
					for (j=i;j;--j) if (c[j]==s[i]) break;
					if (!j)
					{
							puts("-1");
							return;
					}
					for (k=j;k<=min(last,i);++k) c[k]=c[j];
					last=j-1;
			}
		for (i=1;i<=n;++i) if (c[i]!=s[i]) break;
		if (i>n) break;
	}
	printf("%d\n",ans); 
}
int main()
{
		scanf("%d",&n); 
		scanf("%s",c+1);
		scanf("%s",s+1);
		for (i=1;i<=n;++i) if (c[i]!=s[i]) break;
		if (i>n)
		{
				puts("0");
				return 0;
		}
		for (i=1;i<=n;i=j)
		{
			++tot;
			for (j=i;s[j]==s[i]&&j<=n;++j);
			title[tot]=s[i];
			Str[tot]=i;
			End[tot]=j-1;
		}
		int now=n,ans=0;
		for (i=tot;i;--i)
		{
				while (now&&(now>Str[i]||c[now]!=title[i])) --now;	
				if (!now) 
				{
						puts("-1");
						return 0;
				}
				ID[i]=now; 
				vis[now]=1;
		}
		f[tot]=(ID[tot]!=Str[tot]);
		g[tot]=1;
		now=tot;
		for (i=n;i;--i)
			if (!vis[i]) full[i]=full[i+1];
			else 
			{
					full[i]=full[i+1]+1;
					if (now!=tot)
					{
						int needgo=full[i+1]-full[Str[now]+1];
						if (needgo==0) f[now]=(ID[now]!=Str[now]);
						else ques[Str[now]+1].pb(mk(needgo,now));
					}
					--now;
			}
		for (i=n;i;--i)
		{
			if (!vis[i])
			{
					++sum;
					kong[1-sum+n]=i;
			}
			for (j=0;j<(int)ques[i].size();++j)
					T[ques[i][j].se]=kong[ques[i][j].fi-sum+n];
		}
		now=tot;
		for (i=n;i;--i)
			if (vis[i])
			{
					if (now!=tot)
					{
						int needgo=full[i+1]-full[Str[now]+1];
						if (needgo==0) f[now]=(ID[now]!=Str[now]);
						else 
						{
							int pos=T[now];
							f[now]=full[i+1]-full[pos]+1;
						}
						if (f[now]==1&&f[now+1]==0) g[now]=1;
						else
						g[now]=max(f[now]+(Str[now]!=End[now]),f[now+1]+1);
					}
					--now;
			}
		for (i=1;i<=tot;++i) ans=max(ans,g[i]);
		printf("%d\n",ans);
}