概述
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+(m−1)p where
q
+
(
m
−
1
)
p
≤
n
q+(m−1)p≤n
q+(m−1)p≤n and
q
≥
1
q≥1
q≥1.
Input
The first line contains only one integer
T
≤
100
T≤100
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 ≤ 1 0 6 , 1 ≤ m ≤ 1 0 6 1≤n≤10^6,1≤m≤10^6 1≤n≤106,1≤m≤106 and 1 ≤ p ≤ 1 0 6 1≤p≤10^6 1≤p≤106.
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(1≤ai≤109).
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(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
题意:
给
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+(m−1)p 完全一致
题解:
kmp,把
a
a
a中的元素按照
i
i%p
i归类,然后对于每个类当成一个串来用kmp统计这个串中有多少个与
b
b
b完全一致的子串。
注意:用kmp统计符合条件的子串个数的时候,每匹配到一个子串就要将指针向下移动一格。不然会重复计算。
#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 I所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复