zoukankan      html  css  js  c++  java
  • 基础KMP两道

    1. HDU 1711 Number Sequence  

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define N 10007
    
    int a[1000007],b[N],next[N];
    int n,m;
    
    void getnext()
    {
        next[0] = -1;
        int i = 0,j = -1;
        while(i<m)
        {
            if(j == -1 || b[i] == b[j])
            {
                ++i,++j;
                if(b[i] != b[j])
                    next[i] = j;
                else
                    next[i] = next[j];
            }
            else
                j = next[j];
        }
    }
    
    int kmp()
    {
        int i = -1,j = -1;
        while(i<n && j<m)
        {
            if(j == -1 || a[i] == b[j])
                i++,j++;
            else
                j = next[j];
        }
        if(j == m)
            return i-j+1;
        return 0;
    }
    
    int main()
    {
        int t,i,res;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(i=0;i<n;i++)
                scanf("%d",&a[i]);
            for(i=0;i<m;i++)
                scanf("%d",&b[i]);
            if(n<m)
            {
                printf("-1
    ");
                continue;
            }
            getnext();
            res = kmp();
            if(res)
                printf("%d
    ",res);
            else
                printf("-1
    ");
        }
        return 0;
    }
    View Code

    代码2:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define N 10007
    
    int a[1000007],b[N],next[N];
    int n,m;
    
    void getnext()
    {
        next[0] = -1;
        int i = 0,j = -1;
        while(i<m-1)
        {
            if(j == -1 || b[i] == b[j])
                next[++i] = ++j;
            else
                j = next[j];
        }
    }
    
    int kmp()
    {
        int i = -1,j = -1;
        while(i<n && j<m)
        {
            if(j == -1 || a[i] == b[j])
                i++,j++;
            else
                j = next[j];
        }
        if(j == m)
            return i-j+1;
        return 0;
    }
    
    int main()
    {
        int t,i,res;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(i=0;i<n;i++)
                scanf("%d",&a[i]);
            for(i=0;i<m;i++)
                scanf("%d",&b[i]);
            if(n<m)
            {
                printf("-1
    ");
                continue;
            }
            getnext();
            res = kmp();
            if(res)
                printf("%d
    ",res);
            else
                printf("-1
    ");
        }
        return 0;
    }
    View Code

    2.POJ 3461 Oulipo

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define N 10007
    
    char a[1000004],b[N];
    int next[N];
    int n,m,cnt;
    
    void getnext()
    {
        next[0] = -1;
        int i = 0,j = -1;
        while(i<m)
        {
            if(j == -1 || b[i] == b[j])
                next[++i] = ++j;
            else
                j = next[j];
        }
    }
    
    void kmp()
    {
        int i = -1,j = -1;
        while(i<n && j<m)
        {
            if(j == -1 || a[i] == b[j])
                i++,j++;
            else
                j = next[j];
            if(j == m)
            {
                cnt++;
                j = next[j];
            }
        }
    }
    
    int main()
    {
        int t,i;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%s",b);
            scanf("%s",a);
            n = strlen(a);
            m = strlen(b);
            getnext();
            cnt = 0;
            kmp();
            printf("%d
    ",cnt);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    基于mini2440的boa服务器移植
    主机+虚拟机ubuntu+mini2440开发板互相ping通
    poj3133 插头dp
    2015 北京网络赛 E Border Length hihoCoder 1231 树状数组 (2015-11-05 09:30)
    2015 北京网络赛 C Protecting Homeless Cats hihoCoder 1229 树状数组
    acm 2015北京网络赛 F Couple Trees 主席树+树链剖分
    hdu4777 树状数组
    hdu5517 二维树状数组
    Codeforces Round #327 (Div. 1) D. Top Secret Task
    2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You
  • 原文地址:https://www.cnblogs.com/whatbeg/p/3555930.html
Copyright © 2011-2022 走看看