Language: C++

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char s[1000010],t[1000010];
int n;
int next[26][1000001];
int L[1000001],R[1000001],cnt;
int main(){
	scanf("%d%s",&n,s+1);
	for(int i=1;i<=n;i++){
		for(int j=0;j<26;j++)
		next[j][i]=next[j][i-1];
		next[s[i]-'a'][i]=i;
	}
	scanf("%s",t+1);
	if(strcmp(s+1,t+1)==0){
		printf("0\n");
		return 0;
	}
	int pt=n,r=n;
	while(pt>=1){
		int z=t[pt]-'a';
		cnt++;
		R[cnt]=pt;
		while(pt>=1&&t[pt]-'a'==z)pt--;
		int x=next[z][min(r,pt+1)];
		L[cnt]=x;
		if(x==0){
			printf("-1\n");
			return 0;
		}
		r=x-1;
	}
	int ans=0,tmp=1,smg=0,msmg=0;
	for(int i=2;i<=cnt;i++)
	if(R[i]>=L[i-1]){
		tmp++;
		if(L[i-1]-L[i]==R[i-1]-R[i])smg++;
		else {msmg=max(msmg,smg);smg=0;}
	}
	else{
	msmg=max(msmg,smg);
	ans=max(ans,min(tmp,2)+msmg-(msmg==tmp-1));tmp=1;msmg=0;smg=0;
	}
	msmg=max(msmg,smg);
	ans=max(ans,min(tmp,2)+msmg-(msmg==tmp-1));
	printf("%d\n",ans);
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:13:17: error: reference to 'next' is ambiguous
   13 |                 next[j][i]=next[j][i-1];
      |                 ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [26][1000001]'
    7 | int next[26][1000001];
      |     ^~~~
a.cc:13:28: error: reference to 'next' is ambiguous
   13 |                 next[j][i]=next[j][i-1];
      |                            ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [26][1000001]'
    7 | int next[26][1000001];
      |     ^~~~
a.cc:14:17: error: reference to 'next' is ambiguous
   14 |                 next[s[i]-'a'][i]=i;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [26][1000001]'
    7 | int next[26][1000001];
      |     ^~~~
a.cc:27:23: error: reference to 'next' is ambiguous
   27 |                 int x=next[z][min(r,pt+1)];
      |                       ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [26][1000001]'
    7 | int next[26][1000001];
      |     ^~~~