• HDU 5918 Sequence I KMP


    Sequence I



    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,,bmis exactly the sequence aq,aq+p,aq+2p,,aq+(m1)p where q+(m1)pn and q1.
     
    Input
     
    The first line contains only one integer T100, which indicates the number of test cases.

    Each test case contains three lines.

    The first line contains three space-separated integers 1n106,1m106 and 1p106.

    The second line contains n integers a1,a2,,an(1ai109).

    the third line contains m integers 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
     

    题意:

      给你a,b两个序列

      和一个p ,求有多少个 q恰好满足 b1,b2,b3....bm 就是 a[q],a[q+p],a[q+2p]......a[q+(m-1)p];

    题解:

      将a序列,每隔p位置分成一组,这样最多有p组,个数和是n

      将每组和b序列跑KMP计算答案

    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18;
    const double Pi = acos(-1.0);
    const int N = 1e6+10, M = 1e6, mod = 1e9+7, inf = 2e9;
    
    
    int T,n,m,p,s[N],t[N],ans = 0;
    vector<int > P[N];
    int nex[N];
    int main()
    {
        int cas = 1;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%d",&n,&m,&p);
            for(int i = 1; i <= n; ++i) scanf("%d",&t[i]);
            for(int i = 1; i <= m; ++i) scanf("%d",&s[i]);
            for(int i = 0; i < p; ++i) P[i].clear();
            memset(nex,0,sizeof(nex));
            ans = 0;
            int k = 0;
            for(int i=2; i<=m; i++)
            {
                while(k>0&&s[k+1]!=s[i]) k = nex[k];
                if(s[k+1]==s[i])k++;
                nex[i] = k;
            }
            if(p == 1)
            {
                k = 0;
                for(int i=1; i<=n; i++)
                {
                    while(k>0&&s[k+1]!=t[i]) k = nex[k];
                    if(s[k+1]==t[i]) k++;
                    if(k==m) {
                        k = nex[k];
                        ans++;
                    }
                }
                printf("Case #%d: ",cas++);
                printf("%d
    ",ans);
            }
            else {
                for(int i = 1; i <= n; ++i)
                    P[i % p].push_back(t[i]);
                for(int i = 0; i < p; ++i) {
                    k = 0;
                    for(int j = 0; j < P[i].size(); ++j) {
                        while(k>0&&s[k+1]!=P[i][j]) k = nex[k];
                        if(s[k+1]==P[i][j]) k++;
                        if(k==m) {
                            k = nex[k];
                            ans++;
                        }
                    }
                }
                printf("Case #%d: ",cas++);
                printf("%d
    ",ans);
    
            }
        }
    }
  • 相关阅读:
    C#与C++中struct和class的小结
    C#中string的小结
    树的一些操作——遍历,前序和中序建立后续
    一个快速、高效的Levenshtein算法实现——代码实现
    整数拆分
    阶乘结果中0的个数
    普莱菲尔密码矩阵生成算法
    CTF密码学总结
    盲文对照表
    实验吧-古典密码
  • 原文地址:https://www.cnblogs.com/zxhl/p/5931233.html
Copyright © 2020-2023  润新知