rt,本地没有问题,洛谷ide会少输出一行,交的是P3375
#include <bits/stdc++.h>
using namespace std;
string s, str;
int nex[1000010], f[1000010];
void getnext(int nex[], string s) {
for(int i = 1, j = 0; i < s.size(); ++i) {
while(j && s[j] != s[i]) j = nex[j - 1]; //nex匹配失败,指针j回退
if(s[i] == s[j]) ++j; //i会在for循环增加
nex[i] = j;
}
}
void KMP(string s, string str) {
if(str.size() == 0) return;
for(int i = 0, j = 0; i < s.size(); ++i) {
while(j && s[i] != str[j]) j = nex[j - 1]; //失败,回退
if(s[i] == str[j]) ++j; //同上
f[i] = j;
if(j == str.size()) printf("%d\n", i - str.size() + 2);
}
}
int main() {
getline(cin, s);
getline(cin, str);
getnext(nex, str);
KMP(s, str);
// for(int i = 0; i < 9; ++i) printf("%d ", f[i]);
// printf("\n");
for(int i = 0; i < str.size(); ++i) printf("%d ", nex[i]);
return 0;
}