Codeforces Round #269 (Div. 2) D kmp

本文介绍了一个关于塔的高度匹配的问题,通过调整塔的高度,寻找与目标序列相匹配的连续子序列。利用KMP算法高效解决该问题,并给出了详细的实现思路与代码。



链接:戳这里


D. MUH and Cube Walls
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.


题意:

给出n座塔的高度ai,以及m座塔的高度bi。

可以任意多次同时,升高或者下降所有的n座塔。

找出有多少次可以使得,当前下降或升高完之后n座塔出现一段连续的高度与bi完全匹配


思路:

由于可以任意同时上升或者下降一定的高度去匹配,所以每个塔都可以作为开始匹配的点

发现n座塔的相对高度是不变的,不管怎么调整,都是唯一的。

然后这些相对的高度,去匹配m座塔bi,找出有多少可以匹配的连续塔。嗯 这就是kmp了


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n,m;
ll a[200100],b[200100];
int f[200100];
void getfail(){
    int j=0;
    f[0]=f[1]=0;
    for(int i=1;i<m;i++){
        j=f[i];
        while(j && b[j]!=b[i]) j=f[j];
        if(b[i]==b[j]) f[i+1]=j+1;
        else f[j+1]=0;
    }
}
void KMP(){
    n--;m--;
    getfail();
    int ans=0;
    int j=0;
    for(int i=0;i<n;i++){
        while(j && b[j]!=a[i]) j=f[j];
        if(a[i]==b[j]) j++;
        if(j==m) ans++;
    }
    printf("%d\n",ans);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
    for(int i=0;i<m;i++) scanf("%I64d",&b[i]);
    if(m>n) printf("0\n");
    else {
        if(m==1) printf("%d\n",n);
        else {
            for(int i=1;i<n;i++) a[i-1]=a[i]-a[i-1];
            for(int i=1;i<m;i++) b[i-1]=b[i]-b[i-1];
            KMP();
        }
    }
    return 0;
}


先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值