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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef pair<double,int>Q;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,cnt;
string s,t;
vector<int>pos[26];
vector<P>vec;
int val[1000005],ans;
int main(){
	cin>>n>>s>>t;
	for(int i=0;i<n;i++) pos[s[i]-'a'].pb(i);
	for(int i=0;i<t.size();){
		int nxt = t.size();
		for(int j=i;j<t.size();j++){
			if(t[j] != t[i]){
				nxt = j; break;
			}
		}
		//i...nxt-1
		//cout<<i<<" "<<nxt-1<<endl;
		int x = POSU(pos[t[i]-'a'],i);
		x--;
		if(x == -1){
			puts("-1"); return 0;
		}
		for(int y=x;y<pos[t[i]-'a'].size();y++){
			int g = pos[t[i]-'a'][y]; if(g >= nxt) break;
			int h;
			if(y==pos[t[i]-'a'].size()-1){
				h = nxt-1;
			}
			else{
				if(pos[t[i]-'a'][y+1]>=nxt-1) h = nxt-1;
				else h = pos[t[i]-'a'][y+1]-1;
			}
			if(g<h){
				val[g]++; val[h+1]--;
			}
		}
		i=nxt;
	}
	for(int i=1;i<n;i++) val[i] += val[i-1];
	for(int i=0;i<n;i++) ans=max(ans,val[i]);
	cout<<ans<<endl;
}