我是靠谱客的博主 开心树叶,这篇文章主要介绍hdu 5918 Sequence I (kmp),现在分享给大家,希望可以做个参考。

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)这个是可以想清楚的,但是改变之后复杂度应该是会相应增大一些,不是很清楚增大的幅度,结果写的时候没法好好估计时间,感觉会是个很浪费时间的点。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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: %dn", i, ans); } return 0; }

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

最后

以上就是开心树叶最近收集整理的关于hdu 5918 Sequence I (kmp)的全部内容,更多相关hdu内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部