求调WA0
查看原帖
求调WA0
716260
SegmentTree_楼主2024/4/12 17:26
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6+5;
string s1, s2;
int len1, len2;
int nxt[N];
int main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> s1 >> s2;
	len1 = s1.size(), len2 = s2.size();
	int j;
	nxt[0] = 0;
	for (int i = 1;i < len2;i++){
		j = nxt[i - 1];
		while (j && s2[i] != s2[j]) j = nxt[j - 1];
		if (s2[i] == s2[j]) nxt[i] = j + 1;
		else nxt[i] = 0;
	}
	j = 0;
	for (int i = 0;i < len1;i++){
		while (j && s1[i] != s2[j]) j = nxt[j];
		if (s2[j] == s1[i]) ++j;
		if (j == len2){
			cout << (i - len2 + 1) + 1 << '\n';
			j = nxt[j] + 1;
		}
		//cout << i << ' ' << j << '\n';
	}
	for (int i = 0;i < len2;i++) cout << nxt[i] << ' ';
	cout << '\n';
	return 0;
} 
2024/4/12 17:26
加载中...