求助,文件输入输出自测可以过,标准输入输出过不了
查看原帖
求助,文件输入输出自测可以过,标准输入输出过不了
238826
MTEzNUc3楼主2020/8/6 11:51
#include<bits/stdc++.h>
#define maxn 1000010 

using namespace std;

void getfail(char *p,int *f)
{
	int m=strlen(p);
	f[0]=0;
	f[1]=0;
	for(int i=1;i<m;++i)
	{
		int j=f[i];
		while(j&&p[i]!=p[j])j=f[j];
		f[i+1]=(p[i]==p[j])?j+1:0;
	}
}
void find(char *t,char *p,int *f)
{
	int n=strlen(t);
	int m=strlen(p);
	getfail(p,f);
	int j=0;
	for(int i=0;i<n;++i)
	{
		while(j&&p[j]!=t[i])j=f[j];
		if(p[j]==t[i])++j;
		if(j==m)printf("%d\n",i-m+2);
	}
}
int f[maxn];
char p[maxn],t[maxn];
int main()
{
	ios::sync_with_stdio(false);
//	freopen("P3375.in","r",stdin); 
//	freopen("P3375.out","w",stdout); 
	cin>>t;
	cin>>p;	
	find(t,p,f);
	for(int i=1;i<=strlen(p);++i)
	{
		cout<<f[i]<<" ";  
	}
}

2020/8/6 11:51
加载中...