串都是从1开始的
void Kmp(int a[] , int b[] , int n , int m)//a目的目,b匹配串,n目的串数目,m匹配串数目
{
if(m > n) return ;
int pos = 0 ; nxt[1] = 0;
for(int i = 2 ; i <= m ; i++){
while(pos > 0 && b[pos + 1] != b[i]) pos = nxt[pos];//一次匹配失败,往后移动,nxt数组位,pos表示当前最长后缀已经匹配了多少最长前缀了
if(b[pos + 1] == b[i]) pos = pos + 1;
nxt[i] = pos;
}
pos = 0;
for(int i = 1 ; i <= n ; i++){
while(pos > 0 && a[i] != b[pos + 1]) pos = nxt[pos];
if(a[i] == b[pos + 1]) pos = pos + 1;
if(pos == m) ans++ ,pos = nxt[pos];
}
}