471D - MUH and Cube Walls (KMP)

本文介绍了一个有趣的算法问题,即如何在一个由高度不同的塔组成的熊墙中找到与另一组固定模式(大象墙)相匹配的连续塔段。通过对比两堵墙的高度变化来确定匹配的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.

Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of n towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).

Your task is to count the number of segments where Horace can "see an elephant".

Input

The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.

Output

Print the number of segments in the bears' wall where Horace can "see an elephant".

Examples
input
13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2
output
2
Note

The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.

 此方法字符数组和数组求子串的个数是一样的
#include<cstdio>
#include<math.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<stdlib.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define maxn 300005
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
const int mod=1e9+7;
using namespace std;
int a[maxn],b[maxn],f[maxn];
int s1[maxn],s2[maxn];
 int n,w;
void getnexta()
{
	memset(f,0,sizeof(f));
	int k = -1,j = 0;
	f[0] = -1;
	while(j < w)
	{
		if(k == -1||s2[k] == s2[j])
		{
			f[j + 1] = k + 1;
			j ++;
			k ++;
		}
		else
		{
			k = f[k];
		}
	}
}
int kmp()
{
	int ans= 0;
	getnexta();
	int i = 0,j = 0;
	while(i < n && j < w)
	{
		if(j == -1 || s1[i] == s2[j])
		{
			i ++;
			j ++;
		}
		else
		{
			 j = f[j];
		}
		if(j == w)
		{
			ans ++;
			j = f[j];
		}
	}
	return ans;
}
int main()
{

    while(~scanf("%d%d",&n,&w))//从左减右
    {
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        for(int i=0; i<w; i++)
            scanf("%d",&b[i]);
        if(w==1)
        {
            printf("%d\n",n);
        }
        else
        {
            w--,n--;
            for(int i=0; i<n; i++)
            {
                s1[i]=(a[i]-a[i+1]);
            }
            for(int i=0; i<w; i++)
            {
                s2[i]=(b[i]-b[i+1]);
            }
            printf("%d\n",kmp());
        }
    }
    return 0;
}
这种next的数组定义就和字符数组不一样,上面的那个符合
4 2
2 2 2 2
2 2
输出的是 3
下面的这个却是 2 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <iostream>
#include <string>
const int maxn=300005;
using namespace std;
int f[maxn];
int a[maxn],s[maxn], c[maxn],d[maxn];
int n,w;
void getfill()
{
    memset(f,0,sizeof(f));  //根据其前一个字母得到
    for(int i=1; i<w; i++)
    {
        int j=f[i];
        while(j && s[i]!=s[j])
            j=f[j];
        f[i+1]=(s[i]==s[j])?j+1:0;
    }
}
int kmp()
{
    int ans=0;
    getfill();
    int j=0;
    for(int i=0; i<n; i++)
    {
        while(j && a[i]!=s[j])
            j=f[j];
        if(a[i]==s[j])
            j++;
        if(j==w)
        {
            ans++;
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&w))
    {
        for(int i=0; i<n; i++)
            scanf("%d",&c[i]);
        for(int i=0; i<w; i++)
            scanf("%d",&d[i]);
        if(w==1)
            printf("%d\n",n);
        else
        {
            n--;w--;
            for(int i=0; i<n; i++)
            {
                a[i]=c[i]-c[i+1];
            }
            for(int i=0; i<w; i++)
            {
                s[i]=d[i]-d[i+1];
            }
            printf("%d\n",kmp());
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值