#12WA求助
查看原帖
#12WA求助
587909
heyMoonquakes楼主2022/3/19 13:48
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
const int L = 1e6+10;
void KMP(char *S, char *T, int next[]) {
	int i = 0, j = 0, flag = 0;
	int lenS = strlen(S);
	int lenT = strlen(T);
	while (i < lenS) {
		if (S[i] == T[j] || j == -1) {
			i++;
			j++;
		}
		else {
			j = next[j];
		}
		if (j == lenT) {
			flag = 1;
			cout << i - j + 1 << endl;
			j = next[j];
		}
	}
	if (flag == 0) {
		cout << -1;
	}
	return;
}
void getNext(char *T, int* next) {
	int i = 0, j = -1;
	next[0] = -1;
	int lenT =strlen(T);
	while (i < lenT) {
		if (j == -1 || T[i] == T[j]) {
			i++;
			j++;
			next[i] = j;
		}
		else {
			j = next[j];
		}
	}
	return;
}
int main() {
	char s1[L], s2[L];
	scanf("%s",s1);
	scanf("%s",s2);
	int next[L]={0};
	getNext(s2, next);
	KMP(s1, s2, next);
	int i=0,len=strlen(s2);
	for(i=1;i<len;i++){
		cout<<next[i]<<" ";
	}
	cout<<next[i]<<endl;
	return 0;

}
2022/3/19 13:48
加载中...