求助
查看原帖
求助
539583
_huangweiliang_楼主2022/8/17 19:39
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N = 1e6 + 10;
int nxt[N];
string st1, st2;
void get_nxt(){
	int x = 0, y;
	nxt[0] = y = -1;
	while(x < st2.size())
		if(y == -1 || st2[x] == st2[y])
			nxt[++x] == ++y;
		else
			y = nxt[y];
}
void kmp(){
	int x = 0, y = 0;
	while(x < st1.size()){
		if(y == -1 || st1[x] == st2[y]){
			x++;
			y++;
		}
		else
			y == nxt[y];
		if(y == st2.size()){
			cout << x - st2.size() + 1 << endl;
			y = nxt[y];
		}
	}
}

signed main(){
	cin >> st1;
	cin >> st2;
	get_nxt();
	kmp();
	for(int i = 1; i <= st2.size(); i++){
		cout << nxt[i] << ' ';
	}
	return 0;
}//by hwl
2022/8/17 19:39
加载中...