#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000;
int nxt[maxn],ans[maxn],tot;
string s,p;
inline void GetNext(){
nxt[0] = -1;
int pLen = p.size();
int k = -1;
int j = 0;
while(j < pLen){
if(k == -1 || p[k] == p[j]){
j++;
k++;
nxt[j] = k;
}
else{
k = nxt[k];
}
}
}
inline void KmpSearch(int i){
int j = 0;
int pLen = p.size();
int sLen = s.size();
while(i < sLen && j < pLen){
if(j == -1 || s[i] == p[j]){
i++;
j++;
}
else{
j = nxt[j];
}
}
if(j == pLen){
ans[tot++] = i - j + 1;
KmpSearch(i - j + 1);
}
}
int main(){
cin >> s >> p;
GetNext();
KmpSearch(0);
for(register int i = 0;i < tot;i++){
cout << ans[i] << endl;
}
for(register int i = 0;i < p.size();i++){
cout << nxt[i + 1] << " ";
}
return 0;
}
觉得写的挺对的。。。
卡第11和13个点