求助,第11,13超时
查看原帖
求助,第11,13超时
362238
mattinas楼主2021/8/9 14:57
#include<bits/stdc++.h>
using namespace std;
char s1[1000001];
char s2[1000001];
int next[1000001];
void getnext()
{
	int i=-1,j=0;//前缀与后缀 
	next[0]=-1;
	while(j<strlen(s2))
	{
		if(i==-1||s2[i]==s2[j])
		{
			i++;j++;
			next[j]=i;
		}
		else i=next[i];
	}
}
int main()
{
//	freopen("1.in","r",stdin);
//	freopen("1.out","w",stdout); 
	scanf("%s%s",s1,s2);
	getnext();int i=0,j=0;
	int l=strlen(s2);
	while(i<strlen(s1))
	{
		if(j==-1||s1[i]==s2[j])
		{
			i++,j++;
			
		}
		else j=next[j];
		if(j==l)
		{
			printf("%d\n",i-j+1);
		}
	}
	for(int i=1;i<=l;i++)printf("%d ",next[i]);
	return 0;
}
2021/8/9 14:57
加载中...