hdu 5918 Sequence I (kmp)

2 篇文章 0 订阅

Sequence I

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description
Mr. Frog has two sequences a1,a2,⋯,an and b1,b2,⋯,bm and a number p. He wants to know the number of positions q such that sequence b1,b2,⋯,bm is exactly the sequence aq,aq+p,aq+2p,⋯,aq+(m−1)p where q+(m−1)p≤n and q≥1.

Input
The first line contains only one integer T≤100, which indicates the number of test cases.

Each test case contains three lines.

The first line contains three space-separated integers 1≤n≤106,1≤m≤106 and 1≤p≤106.

The second line contains n integers a1,a2,⋯,an(1≤ai≤109).

the third line contains m integers b1,b2,⋯,bm(1≤bi≤109).

Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the number of valid q’s.

Sample Input
2
6 3 1
1 2 3 1 2 3
1 2 3
6 3 2
1 3 2 2 3 1
1 2 3

Sample Output
Case #1: 2
Case #2: 1

这题本来是觉得似乎不能用kmp,结果重现赛所有的CE都是kmp。然后我就开始方了。

然后开始思考,kmp模板里面的j = nxt[m]这个部分如果直接套用肯定会跳过一些可能的匹配导致出错,但只考虑和模式串的匹配的话,kmp还是有用武之地的。

于是把kmp胡改一通,枚举所有匹配起点,每次匹配完一个模式串长度,原串匹配下标加一,模式串下标变0重新开始。

本以为会T,结果并没有。对kmp的复杂度一直都不是很懂,改变之前最大复杂度是O(n + m)这个是可以想清楚的,但是改变之后复杂度应该是会相应增大一些,不是很清楚增大的幅度,结果写的时候没法好好估计时间,感觉会是个很浪费时间的点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#define MAX 1000010
#define M 15
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-8

using namespace std;

int t, n, m, p, ans;

int txt[MAX], nxt[MAX], T[MAX];

void getnxt()//模板
{
    nxt[0] = -1;
    int i = 0, j = -1;
    while(i < m)
    {
        while(j >= 0 && txt[i] != txt[j])
            j = nxt[j];
        j++;
        i++;
        if(txt[i] == txt[j])
            nxt[i] = nxt[j];
        else
            nxt[i] = j;
    }
}

void kmp(int p)
{
    getnxt();
    int i = 0, j = 0, cnt = 0;
    while(i < n)
    {
        while(j >= 0 && T[i] != txt[j])
            j = nxt[j];
        i += p; cnt++;//与模板不同的部分
        j++;
        if(j == m)//与模板不同的部分
        {
            ans++;
            j = 0;
            i -= m * p;
            i++;
            cnt = 0;
        }
        else if(cnt >= m)//比模板增加的部分
        {
            i -= m * p;
            i++;
            j = 0;
            cnt = 0;
        }
    }
}

int main()
{
    scanf("%d", &t);
    for(int i = 1; i <= t; i++)
    {
        ans = 0;
        scanf("%d %d %d", &n, &m, &p);
        for(int j = 0; j < n; j++)  scanf("%d", &T[j]);
        for(int j = 0; j < m; j++)  scanf("%d", &txt[j]);
        kmp(p);
        printf("Case #%d: %d\n", i, ans);
    }
    return 0;
}

运行结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值