您的位置:首页 > 产品设计 > UI/UE

hdu 5918 Sequence I (kmp)

2016-10-04 20:10 316 查看

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;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  kmp