[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

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+(m1)p 完全一致
题解:
kmp,把 a a a中的元素按照 i i%p i归类,然后对于每个类当成一个串来用kmp统计这个串中有多少个与 b b b完全一致的子串。
注意:用kmp统计符合条件的子串个数的时候,每匹配到一个子串就要将指针向下移动一格。不然会重复计算。

#include<bits/stdc++.h>
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: %d\n",CASE,ans);
    return 0;
}
int main(){
    int T;scanf("%d",&T);
    for(int i=1;i<=T;i++)w33ha(i);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值