zoukankan      html  css  js  c++  java
  • HDU-1686-Oulipo

    链接:https://vjudge.net/problem/HDU-1686#author=0

    题意:

    求模式串在待匹配串的出现次数。

    思路:

    kmp

    代码:

    #include <iostream>
    #include <memory.h>
    #include <vector>
    #include <map>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <queue>
    #include <string>
    
    using namespace std;
    
    typedef long long LL;
    
    const int MAXN = 1e6 + 10;
    const int MAXM = 1e4 + 10;
    
    int Next[MAXM];
    string a, b;
    
    void Get_Next(int m)
    {
        int k = -1;
        int j = 0;
        Next[j] = k;
        while (j < m)
        {
            if (k == -1 || b[j] == b[k])
            {
                j++;
                k++;
                Next[j] = k;
            }
            else
                k = Next[k];
        }
    }
    
    int Kmp(int n, int m)
    {
        int i = 0;
        int j = 0;
        int res = 0;
        Get_Next(m);
        while (i < n)
        {
            if (j == -1 || a[i] == b[j])
                i++, j++;
            else
                j = Next[j];
            if (j == m)
                res++;
        }
        return res;
    }
    
    int main()
    {
        int t, n, m;
        cin >> t;
        while (t--)
        {
            memset(Next, 0, sizeof(Next));
            cin >> b >> a;
            cout << Kmp(a.length(), b.length()) << endl;
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    第一部分 android display(sufaceflinger & overlay)
    UML类图关系大全
    第二部分 MediaPlayer的接口与架构
    Climbing Stairs
    Add Binary
    Plus One
    Unique Paths
    Length of Last Word
    Count and Say
    Valid Sudoku
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10552649.html
Copyright © 2011-2022 走看看