我感觉用差分算没错啊,为什么输出总是0啊
查看原帖
我感觉用差分算没错啊,为什么输出总是0啊
751062
Diary_Of_Young楼主2024/8/14 22:42
#include<bits/stdc++.h>

using  namespace std;

const int N=1e6+10;
int stand[N],str[N],temp[N];
int ne[N];
int first,he[N];

void kmp_next(int *str)
{
	int len = sizeof(str)/4;
	ne[1] = 0;
	for(int i = 2,j = 0;i <= len;i ++){
		while(j&&str[i] != str[j+1]) j = ne[j];
		if(str[i] == str[j+1]) j ++;
		ne[i] = j;
	}
}

int kmp_search(int *a,int *str)
{
	int len=sizeof(a)/4,cnt=0;
	for(int i=1,j=0;i<=len;i++)
	{
		if(first>=he[i])
		{	
			int p=i+1;
			while(j&&a[p] != str[j+1]) j=ne[j];
			if(a[p] == str[p+1] ) j++;
			if(j==len) cnt++;
		}
	}
	return cnt;
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int m,n;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>he[i];
	for(int i=1;i<=n;i++) stand[i]=he[i]-he[i-1];
	for(int i=1;i<=n;i++) cin>>temp[i];
	first=temp[1];
	for(int i=1;i<=n;i++) str[i]=temp[i]-temp[i-1];

	
	kmp_next(temp);
	cout<<kmp_search(stand,str);
	
	 
	return 0;
} 
2024/8/14 22:42
加载中...