求助一下
查看原帖
求助一下
209761
草薙哮楼主2020/10/22 18:14
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int N=1000100;

int next[N],f[N];
char s1[N],s2[N];
int n,m;
void Next(char *s,int l)
{
	next[1]=0;
	for(int i=2,j=0;i<=l;i++)
	{
		while(j>0&&s[i]!=s[j+1])
		{
			j=next[j];
		 } 
		if(s[i]==s[j+1]) 
		next[i]=++j;
		else next[i]=0;
	}
}
void compare(char *s1, char *s2)
{
	for(int i=1,j=0;i<=n;i++)
	{
		while(j>0&&(s1[i]!=s2[j+1]||j==m)) j=next[j];
		if(s1[i]==s2[j+1])
		f[i]=++j;
		if(f[i]==m) cout<<i-m+1<<endl; 
		
	}
}
int main()
{
	scanf("%s%s",s1+1,s2+1);
	n=strlen(s1+1);
	m=strlen(s2+1);
	Next(s1,n);
	compare(s1,s2);
	Next(s2,m); 
	for(int i=1;i<=m;i++)
	{
		printf("%d ",next[i]);
	}
	
	return 0;
 } 
2020/10/22 18:14
加载中...