测试集和#4都是一样的,就是错,为什么啊?
查看原帖
测试集和#4都是一样的,就是错,为什么啊?
1003019
Kotori_楼主2024/1/19 19:37
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define MAXN 1000010
	char ste[MAXN];
	char str[MAXN];
	int m[MAXN] ;
void KMP(char a[],int long1,int kmp[])
{

	//int kmp[1000001];
	kmp[0] = 0;
	int i = 0,j = 1;
	while(j<=long1){
		if(a[j] != a[i]){
			if(a[j] == a[0]){	kmp[j] = 1;}
			else	kmp[j] = 0;
			i = 0;j++;
		}
		else{	kmp[j] = kmp[j-1]+1;
				j++;i++;}
	}
	return ;
}
int main(){
//	freopen("P3375_4.in","r",stdin); 
//	freopen("P3375_4.out","w",stdout); 
	cin>>ste;
	cin>>str;
	int long1 = strlen(str);int long2 = strlen(ste);

	KMP(str,long1,m);
	//cout<<long2<<endl;
	int i = 0,j = 0;
	while(j<=long2){
		if(i==long1){
			cout<<j-long1+1<<endl;
			//cout<<"数字:"<<j<<endl;
			i = 0;
			j--;
		} 
		if(str[i] == ste[j]) {
			i++;
			j++;
			//cout<<"i:"<<i<<endl;
		}
		else{
			if(i==0) j++;
			i = m[i-1];
		}
	}

	for(int i =0;i<long1;i++){
		cout<<m[i]<<' ';
	}
 	return 0;
}

2024/1/19 19:37
加载中...