我是靠谱客的博主 懵懂果汁,这篇文章主要介绍[HDU5918]Sequence I,现在分享给大家,希望可以做个参考。

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

Problem Description
Mr. Frog has two sequences a 1 , a 2 , ⋯ , a n a_1,a_2,⋯,a_n a1,a2,,an and b 1 , b 2 , ⋯ , b m b_1,b_2,⋯,b_m b1,b2,,bm and a number p p p. He wants to know the number of positions q such that sequence b 1 , b 2 , ⋯ , b m b_1,b_2,⋯,b_m b1,b2,,bm is exactly the sequence a q , a q + p , a q + 2 p , ⋯ , a q + ( m − 1 ) p a_q,a_{q+p},a_{q+2p},⋯,a_{q+(m−1)p} aq,aq+p,aq+2p,,aq+(m1)p where q + ( m − 1 ) p ≤ n q+(m−1)p≤n q+(m1)pn and q ≥ 1 q≥1 q1.

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

Each test case contains three lines.

The first line contains three space-separated integers 1 ≤ n ≤ 1 0 6 , 1 ≤ m ≤ 1 0 6 1≤n≤10^6,1≤m≤10^6 1n106,1m106 and 1 ≤ p ≤ 1 0 6 1≤p≤10^6 1p106.

The second line contains n integers a 1 , a 2 , ⋯ , a n ( 1 ≤ a i ≤ 1 0 9 ) a_1,a_2,⋯,a_n(1≤a_i≤10^9) a1,a2,,an(1ai109).

the third line contains m integers b 1 , b 2 , ⋯ , b m ( 1 ≤ b i ≤ 1 0 9 ) b_1,b_2,⋯,b_m(1≤b_i≤10^9) b1,b2,,bm(1bi109).

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

复制代码
1
2
3
4
5
6
7
8
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

复制代码
1
2
3
Case #1: 2 Case #2: 1

题意:
n , m , p n,m,p n,m,p,再给两个数字串 a , b a,b a,b a a a的长度为 n n n b b b的长度为 m m m。问有多少个 q > = 1 q>=1 q>=1满足 b 1 , b 2 , ⋯ , b m b_1,b_2,⋯,b_m b1,b2,,bm a q , a q + p , a q + 2 p , ⋯ , a q + ( m − 1 ) p a_q,a_{q+p},a_{q+2p},⋯,a_{q+(m−1)p} aq,aq+p,aq+2p,,aq+(m1)p 完全一致
题解:
kmp,把 a a a中的元素按照 i i%p i归类,然后对于每个类当成一个串来用kmp统计这个串中有多少个与 b b b完全一致的子串。
注意:用kmp统计符合条件的子串个数的时候,每匹配到一个子串就要将指针向下移动一格。不然会重复计算。

复制代码
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
#include<bits/stdc++.h> #define LiangJiaJun main #define MOD 19991227 using namespace std; int n,m,p; int a[1000004],b[1000004],nt[1000004]; vector<int>ov[1000004]; int getnext(){ memset(nt,0,sizeof(nt)); int j=0; for(int i=2;i<=m;i++){ while(j>0&&b[j+1]!=b[i])j=nt[j]; if(b[j+1]==b[i])j++; nt[i]=j; } return 0; } int calc(int st){ int j=0,res=0; for(int i=0;i<ov[st].size();i++){ while(j>0&&b[j+1]!=ov[st][i])j=nt[j]; if(b[j+1]==ov[st][i])j++; if(j==m){ res++; j=nt[j];///记住要后移 } } return res; } int w33ha(int CASE){ scanf("%d%d%d",&n,&m,&p); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=m;i++)scanf("%d",&b[i]); getnext(); for(int i=0;i<p;i++)if(ov[i].size()>0)ov[i].clear(); for(int i=1;i<=n;i++)ov[i%p].push_back(a[i]); int ans=0; for(int i=0;i<p;i++){ if(ov[i].size()>=m)ans+=calc(i); } printf("Case #%d: %dn",CASE,ans); return 0; } int LiangJiaJun(){ int T;scanf("%d",&T); for(int i=1;i<=T;i++)w33ha(i); return 0; }

最后

以上就是懵懂果汁最近收集整理的关于[HDU5918]Sequence I的全部内容,更多相关[HDU5918]Sequence内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部