问题遇到的现象和发生背景
VScode 错误显示数组定义不明确
问题相关代码
#include<iostream>
#include<string>
using namespace std;
int next[100010];
void get_next(string S)
{
next[0]=-1;
next[1]=0;
int i=2;
int cn=0;
while(i<S.size())
{
if(S[i-1]==S[cn])next[i++]=++cn;
else if(next[cn]>-1)cn=next[cn];
else next[i++]=0;
}
}
int index_KMP(string S,string T)
{
int i=0,j=0;
while(i<S.size()&&j<T.size())
{
if(S[i]==T[j]){i++;j++;}
else if(next[j]==-1){i++;}
else j=next[j];
}
if(j>=T.size())return i-T.size();
else return -1;
}
int main()
{
string a,b;
cin>>a>>b;
int cnt=index_KMP(a,b);
cout<<cnt<<endl;
return 0;
}