求助,只对了一个点
查看原帖
求助,只对了一个点
555110
icehellfire楼主2022/7/21 22:35
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int next1[1000000];
int n;
string s2,s;
void bulit(int j,int i)
{
//	for(int i = 1;i <n;i++){
//		cout<<next[i]<<" ";
//	}
//	cout<<endl;
	if(j-1<0){
		next1[i] = -1;
	}
	else if(s[i] == s[next1[j-1] + 1]){
		next1[i] = next1[j-1] + 1;
	}
	else if(j-1>0){
		bulit(next1[j-1] + 1,i);
	}
	else{
		next1[i] = -1;
	}
	return;
}
int KMP(){
	int x = 1;
	int i = 0,j = 0;
	while(i<=s2.length()){
		if(j == n){
			cout<<x<<endl;
			x = i;
			i-=1;

			j = 0;
		}
		if(s[j] == s2[i]){
			i++;j++;
		}
		else{
			i++;
			j = next1[j]+1;
			x = i-j+1;
		}	
	}
	return 0;
}
int main()
{

	cin>>s2;
	cin>>s;
	n = s.length();
	next1[0] = -1;
	for(int i = 1;i <n;i++){
		bulit(i,i);
	}
	KMP();
	for(int i = 0;i <n;i++){
		cout<<next1[i]+1<<" ";
	}
	cout<<endl;
	return 0;
 } 
2022/7/21 22:35
加载中...