結果

問題 No.2626 Similar But Different Name
ユーザー ぷらぷら
提出日時 2024-02-09 22:25:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 3,000 ms
コード長 4,818 bytes
コンパイル時間 1,518 ms
コンパイル使用メモリ 149,068 KB
実行使用メモリ 69,704 KB
最終ジャッジ日時 2024-02-09 22:25:35
合計ジャッジ時間 5,699 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 3 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 3 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 176 ms
69,704 KB
testcase_19 AC 90 ms
36,552 KB
testcase_20 AC 87 ms
36,552 KB
testcase_21 AC 82 ms
36,552 KB
testcase_22 AC 167 ms
62,224 KB
testcase_23 AC 184 ms
62,328 KB
testcase_24 AC 166 ms
60,372 KB
testcase_25 AC 163 ms
61,036 KB
testcase_26 AC 165 ms
62,316 KB
testcase_27 AC 169 ms
62,448 KB
testcase_28 AC 170 ms
61,508 KB
testcase_29 AC 185 ms
60,360 KB
testcase_30 AC 164 ms
61,020 KB
testcase_31 AC 166 ms
61,932 KB
testcase_32 AC 169 ms
62,392 KB
testcase_33 AC 162 ms
62,448 KB
testcase_34 AC 164 ms
60,384 KB
testcase_35 AC 166 ms
61,952 KB
testcase_36 AC 168 ms
60,384 KB
testcase_37 AC 164 ms
69,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

vector<int> Z_algorithm(string S) {
    vector<int>Z(S.size());
    Z[0] = S.size();
    int i = 1,j = 0;
    while(i < S.size()) {
        while(i+j < S.size() && S[i+j] == S[j]) {
            j++;
        }
        Z[i] = j;
        if(j == 0) {
            i++;
            continue;
        }
        int k = 1;
        while(k+Z[k] < j) {
            Z[i+k] = Z[k];
            k++;
        }
        i += k;
        j -= k;
    }
    return Z;
}

namespace FastFourierTransform {
using real = double;

struct C {
    real x, y;
    
    C() : x(0), y(0) {}
    
    C(real x, real y) : x(x), y(y) {}
    
    inline C operator+(const C &c) const { return C(x + c.x, y + c.y); }
    
    inline C operator-(const C &c) const { return C(x - c.x, y - c.y); }
    
    inline C operator*(const C &c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); }
    
    inline C conj() const { return C(x, -y); }
};

const real PI = acosl(-1);
int base = 1;
vector< C > rts = { {0, 0},
    {1, 0} };
vector< int > rev = {0, 1};


void ensure_base(int nbase) {
    if(nbase <= base) return;
    rev.resize(1 << nbase);
    rts.resize(1 << nbase);
    for(int i = 0; i < (1 << nbase); i++) {
        rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
    }
    while(base < nbase) {
        real angle = PI * 2.0 / (1 << (base + 1));
        for(int i = 1 << (base - 1); i < (1 << base); i++) {
            rts[i << 1] = rts[i];
            real angle_i = angle * (2 * i + 1 - (1 << base));
            rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i));
        }
        ++base;
    }
}

void fft(vector< C > &a, int n) {
    assert((n & (n - 1)) == 0);
    int zeros = __builtin_ctz(n);
    ensure_base(zeros);
    int shift = base - zeros;
    for(int i = 0; i < n; i++) {
        if(i < (rev[i] >> shift)) {
            swap(a[i], a[rev[i] >> shift]);
        }
    }
    for(int k = 1; k < n; k <<= 1) {
        for(int i = 0; i < n; i += 2 * k) {
            for(int j = 0; j < k; j++) {
                C z = a[i + j + k] * rts[j + k];
                a[i + j + k] = a[i + j] - z;
                a[i + j] = a[i + j] + z;
            }
        }
    }
}

vector< long long > multiply(const vector< int > &a, const vector< int > &b) {
    int need = (int) a.size() + (int) b.size() - 1;
    int nbase = 1;
    while((1 << nbase) < need) nbase++;
    ensure_base(nbase);
    int sz = 1 << nbase;
    vector< C > fa(sz);
    for(int i = 0; i < sz; i++) {
        int x = (i < (int) a.size() ? a[i] : 0);
        int y = (i < (int) b.size() ? b[i] : 0);
        fa[i] = C(x, y);
    }
    fft(fa, sz);
    C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0);
    for(int i = 0; i <= (sz >> 1); i++) {
        int j = (sz - i) & (sz - 1);
        C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r;
        fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r;
        fa[i] = z;
    }
    for(int i = 0; i < (sz >> 1); i++) {
        C A0 = (fa[i] + fa[i + (sz >> 1)]) * t;
        C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i];
        fa[i] = A0 + A1 * s;
    }
    fft(fa, sz >> 1);
    vector< long long > ret(need);
    for(int i = 0; i < need; i++) {
        ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x);
    }
    return ret;
}
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,M,K;
    cin >> N >> M >> K;
    string S,T;
    cin >> S >> T;
    string R = T+S;
    for(int i = 0; i < R.size(); i++) {
        if('A' <= R[i] && R[i] <= 'Z') {
            R[i] = (char)(R[i]-'A'+'a');
        }
    }
    vector<int>a = Z_algorithm(R);
    vector<int>b(N),c(M),b2(N,1),c2(M,1);
    for(int i = 0; i < S.size(); i++) {
        if('A' <= S[i] && S[i] <= 'Z') {
            b[i] = 1;
            b2[i] = 0;
        }
    }
    for(int i = 0; i < T.size(); i++) {
        if('a' <= T[i] && T[i] <= 'z') {
            c[T.size()-i-1] = 1;
            c2[T.size()-i-1] = 0;
        }
    }
    vector<long long>d = FastFourierTransform::multiply(b,c);
    vector<long long>d2 = FastFourierTransform::multiply(b2,c2);
    int ans = 0;
    for(int i = 0; i+M <= N; i++) {
        int now = d[M-1+i]+d2[M-1+i];
        if(a[i+M] >= M && 1 <= now && now <= K) {
            ans++;
        }
    }
    cout << ans << "\n";
}
0