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 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>

#define MAX(x,y) (x>y?x:y)
#define MIN(x,y) (x<y?x:y)

int N;
char S0[1000001];
char T[1000001];
int k[1000001];
int mk[1000001];
int d[1000001];

int calc(){
	int re = -1;
	int i = 0, j = 0;
	for( i = 0; i < N; i++ ){
		d[i] = i-k[i];
		//printf("%d ", d[i]);
	}
	//printf("\n");
	re = d[N-1];
	int temp = re;
	for( i = N-2; i >= 0; i-- ){
		if( d[i] == 0 ){
			if( temp > re ){ re = temp; temp = 0; }
		}else{
			if( T[i] == T[i+1] ){
				//保留。
			}else{
				if( d[i] > temp ){
					temp = d[i];
				}else{
					temp += MAX(d[i+1]+1-d[i], 0);
				}
			}
		}
		printf("%d:%d\n", i, temp);
	}
	//printf("%d\n", re);
	return re;
}

int getki(int i){
	int re = 20000000;
	int j = 0;
	if( i < 0 ){ re = calc(); }
	for( j = k[i+1]; j >= 0; j-- ){
		//if( j < mk[i] ){ break; }
		if( T[i] == S0[j] ){
			k[i] = j;
			int temp = getki(i-1);
			if( temp < 0 ){ continue; }
			re = MIN(temp, re);
			break;
		}
	}
	return re;
}

int main(){
	int re = -1;
	int i = 0, j = 0;
	scanf("%d", &N);
	scanf("%s", S0);
	scanf("%s", T);
	mk[0] = 0;
	for( i = 1; i < N; i++ ){
		for( j = mk[i-1]; j < N; j++ ){
			if( T[i] == S0[j] ){
				mk[i] = j;
				break;
			}
		}
	}
	k[N] = N-1;
	re = getki(N-1);
	/*
	for( i = N-1; i >= 0; i-- ){
		for( j = k[i+1]; j >= 0; j-- ){
			if( T[i] == S0[j] ){
				k[i] = j;
				d[i] = i-j;
				break;
			}
		}
		if( j < 0 ){ re = -1; break; }
	}
	
	if( re != -1 ){
		re = d[N-1];
		int temp = re;
		for( i = N-2; i >= 0; i-- ){
			if( d[i] == 0 ){
				if( temp > re ){ re = temp; }
			}else{
				if( T[i] == T[i+1] ){
					//保留。
				}else{
					if( d[i] > temp ){
						temp = d[i];
					}else{
						temp += MAX(d[i+1]+1-d[i], 0);
					}
				}
			}
		}
		
		
	}
	*/
	/*
	for( i = 0; i < N; i++ ){
		printf("%d ", d[i]);
	}
	printf("\n");
	*/
	if( re > 10000000 ){ re = -1; }
	printf("%d\n", re);
	return 0;
}