在获得next数组基础上,使用next数组来匹配字符串
//
// KMPAlth.cpp
// FirstP
//
// Created by 赫赫 on 2023/11/2.
//
#include "KMPAlth.hpp"
//KMP算法,已经得到了next数组
int index_KMP(string S,string T,int next[]){
int i=1,j=1;
while(i<=S.length()&&j<T.length()){
if(j==0||S.at(i)==T.at(j)){
i++;
j++;
}
else{
j=next[j];
}
}
if(j>T.length()){
return i-(int)T.length();
}else{
return 0;
}
}