POJ3461 HDU1686 SCU2652 Oulipo

題目連結 POJ
題目連結 HDU
題目連結 SCU

  • 題意:給定字串 $W,T$ ,求 $W$ 在 $T$ 出現的次數。
  • 題解:利用 KMP 算出。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    #pragma GCC optimize(2)
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #include <map>
    #include <string>
    #include <vector>
    using namespace std;
    typedef unsigned long long ULL;
    const int INF = 1e9;
    const int MXN = 1e5 + 5;
    const int MXV = 3e5 + 5;
    const ULL MOD = 10009;
    const ULL seed = 31;
    #define MP make_pair
    #define PB push_back
    #define F first
    #define S second
    #define FOR(i, L, R) for (int i = L; i != (int)R; ++i)
    #define FORD(i, L, R) for (int i = L; i != (int)R; --i)
    #define IOS \
    cin.tie(NULL); \
    cout.tie(NULL); \
    ios_base::sync_with_stdio(false);

    int fail[MXN];
    void bulid_fail_funtion(string B)
    {
    int len = B.length(), current_pos;
    current_pos = fail[0] = -1;
    for (int i = 1; i < len; i++)
    {
    while (current_pos != -1 && B[current_pos + 1] != B[i])
    {
    current_pos = fail[current_pos];
    }
    if (B[current_pos + 1] == B[i])
    {
    current_pos++;
    }
    fail[i] = current_pos;
    }
    }

    int match(string A, string B)
    {
    int lenA = A.size(), lenB = B.size();
    int current_pos = -1;
    int ans = 0;
    for (int i = 0; i < lenA; i++)
    {
    while (current_pos != -1 && B[current_pos + 1] != A[i])
    {
    current_pos = fail[current_pos];
    }
    if (B[current_pos + 1] == A[i])
    current_pos++;
    if (current_pos == lenB - 1)
    { // match! A[i-lenB+1,i]=B
    current_pos = fail[current_pos];
    ++ans;
    }
    }
    return ans;
    }
    int main()
    {
    IOS;
    int t;
    cin >> t;
    while (t--)
    {
    string s, r;
    cin >> s >> r;
    bulid_fail_funtion(s);
    cout << match(r, s) << '\n';
    }
    }

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Recommended Posts