結果

問題 No.2626 Similar But Different Name
ユーザー 👑 emthrmemthrm
提出日時 2024-02-09 22:10:01
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 3,000 ms
コード長 6,141 bytes
コンパイル時間 3,285 ms
コンパイル使用メモリ 268,992 KB
実行使用メモリ 53,152 KB
最終ジャッジ日時 2024-02-09 22:10:08
合計ジャッジ時間 6,273 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 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 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 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 112 ms
53,152 KB
testcase_19 AC 56 ms
28,328 KB
testcase_20 AC 56 ms
28,328 KB
testcase_21 AC 50 ms
28,328 KB
testcase_22 AC 102 ms
49,896 KB
testcase_23 AC 102 ms
49,852 KB
testcase_24 AC 110 ms
49,116 KB
testcase_25 AC 129 ms
49,404 KB
testcase_26 AC 106 ms
49,840 KB
testcase_27 AC 108 ms
49,940 KB
testcase_28 AC 106 ms
49,540 KB
testcase_29 AC 106 ms
49,108 KB
testcase_30 AC 106 ms
49,392 KB
testcase_31 AC 112 ms
49,772 KB
testcase_32 AC 98 ms
49,900 KB
testcase_33 AC 108 ms
49,940 KB
testcase_34 AC 97 ms
49,000 KB
testcase_35 AC 130 ms
49,788 KB
testcase_36 AC 95 ms
49,000 KB
testcase_37 AC 113 ms
53,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

namespace fast_fourier_transform {

using Real = double;

struct Complex {
  Real re, im;
  explicit Complex(const Real re = 0, const Real im = 0) : re(re), im(im) {}
  inline Complex operator+(const Complex& x) const {
    return Complex(re + x.re, im + x.im);
  }
  inline Complex operator-(const Complex& x) const {
    return Complex(re - x.re, im - x.im);
  }
  inline Complex operator*(const Complex& x) const {
    return Complex(re * x.re - im * x.im, re * x.im + im * x.re);
  }
  inline Complex mul_real(const Real r) const {
    return Complex(re * r, im * r);
  }
  inline Complex mul_pin(const Real r) const {
    return Complex(-im * r, re * r);
  }
  inline Complex conj() const { return Complex(re, -im); }
};

std::vector<int> butterfly{0};
std::vector<std::vector<Complex>> zeta{{Complex(1, 0)}};

void init(const int n) {
  const int prev_n = butterfly.size();
  if (n <= prev_n) return;
  butterfly.resize(n);
  const int prev_lg = zeta.size();
  const int lg = std::countr_zero(static_cast<unsigned int>(n));
  for (int i = 1; i < prev_n; ++i) {
    butterfly[i] <<= lg - prev_lg;
  }
  for (int i = prev_n; i < n; ++i) {
    butterfly[i] = (butterfly[i >> 1] >> 1) | ((i & 1) << (lg - 1));
  }
  zeta.resize(lg);
  for (int i = prev_lg; i < lg; ++i) {
    zeta[i].resize(1 << i);
    const Real angle = -3.14159265358979323846 * 2 / (1 << (i + 1));
    for (int j = 0; j < (1 << (i - 1)); ++j) {
      zeta[i][j << 1] = zeta[i - 1][j];
      const Real theta = angle * ((j << 1) + 1);
      zeta[i][(j << 1) + 1] = Complex(std::cos(theta), std::sin(theta));
    }
  }
}

void dft(std::vector<Complex>* a) {
  assert(std::has_single_bit(a->size()));
  const int n = a->size();
  init(n);
  const int shift =
      std::countr_zero(butterfly.size()) - std::countr_zero(a->size());
  for (int i = 0; i < n; ++i) {
    const int j = butterfly[i] >> shift;
    if (i < j) std::swap((*a)[i], (*a)[j]);
  }
  for (int block = 1, den = 0; block < n; block <<= 1, ++den) {
    for (int i = 0; i < n; i += (block << 1)) {
      for (int j = 0; j < block; ++j) {
        const Complex tmp = (*a)[i + j + block] * zeta[den][j];
        (*a)[i + j + block] = (*a)[i + j] - tmp;
        (*a)[i + j] = (*a)[i + j] + tmp;
      }
    }
  }
}

template <typename T>
std::vector<Complex> real_dft(const std::vector<T>& a) {
  const int n = a.size();
  std::vector<Complex> c(std::bit_ceil(a.size()));
  for (int i = 0; i < n; ++i) {
    c[i].re = a[i];
  }
  dft(&c);
  return c;
}

void idft(std::vector<Complex>* a) {
  const int n = a->size();
  dft(a);
  std::reverse(std::next(a->begin()), a->end());
  const Real r = 1. / n;
  std::transform(a->begin(), a->end(), a->begin(),
                 [r](const Complex& c) -> Complex { return c.mul_real(r); });
}

template <typename T>
std::vector<Real> convolution(const std::vector<T>& a,
                              const std::vector<T>& b) {
  const int a_size = a.size(), b_size = b.size(), c_size = a_size + b_size - 1;
  const int n = std::max(std::bit_ceil(static_cast<unsigned int>(c_size)), 2U);
  const int hlf = n >> 1, qtr = hlf >> 1;
  std::vector<Complex> c(n);
  for (int i = 0; i < a_size; ++i) {
    c[i].re = a[i];
  }
  for (int i = 0; i < b_size; ++i) {
    c[i].im = b[i];
  }
  dft(&c);
  c.front() = Complex(c.front().re * c.front().im, 0);
  for (int i = 1; i < hlf; ++i) {
    const Complex i_square = c[i] * c[i], j_square = c[n - i] * c[n - i];
    c[i] = (j_square.conj() - i_square).mul_pin(0.25);
    c[n - i] = (i_square.conj() - j_square).mul_pin(0.25);
  }
  c[hlf] = Complex(c[hlf].re * c[hlf].im, 0);
  c.front() = (c.front() + c[hlf]
               + (c.front() - c[hlf]).mul_pin(1)).mul_real(0.5);
  const int den = std::countr_zero(static_cast<unsigned int>(hlf));
  for (int i = 1; i < qtr; ++i) {
    const int j = hlf - i;
    const Complex tmp1 = c[i] + c[j].conj();
    const Complex tmp2 = ((c[i] - c[j].conj()) * zeta[den][j]).mul_pin(1);
    c[i] = (tmp1 - tmp2).mul_real(0.5);
    c[j] = (tmp1 + tmp2).mul_real(0.5).conj();
  }
  if (qtr > 0) c[qtr] = c[qtr].conj();
  c.resize(hlf);
  idft(&c);
  std::vector<Real> res(c_size);
  for (int i = 0; i < c_size; i += 2) {
    res[i] = c[i >> 1].re;
  }
  for (int i = 1; i < c_size; i += 2) {
    res[i] = c[i >> 1].im;
  }
  return res;
}

}  // namespace fast_fourier_transform

template <typename T>
std::vector<int> z_algorithm(const T &s) {
  const int n = s.size();
  std::vector<int> res(n, 0);
  for (int i = 1, j = 0; i < n; ++i) {
    if (i + res[i - j] < j + res[j]) {
      res[i] = res[i - j];
    } else {
      res[i] = std::max(j + res[j] - i, 0);
      while (i + res[i] < n && s[i + res[i]] == s[res[i]]) ++res[i];
      j = i;
    }
  }
  res[0] = n;
  return res;
}

int main() {
  int n, m, k; string s, t; cin >> n >> m >> k >> s >> t;
  vector<int> a(n), b(m);
  REP(i, n) a[i] = (islower(s[i]) ? 1 : -1);
  REP(i, m) b[i] = (islower(t[i]) ? 1 : -1);
  ranges::reverse(b);
  const auto c = fast_fourier_transform::convolution(a, b);
  for (char& c : s) c = tolower(c);
  for (char& c : t) c = tolower(c);
  const vector<int> z = z_algorithm(t + '$' + s);
  int ans = 0;
  REP(i, n) {
    if (z[m + 1 + i] == m && m - k * 2 <= llround(c[m - 1 + i]) && llround(c[m - 1 + i]) <= m - 2) ++ans;
  }
  cout << ans << '\n';
  return 0;
}
0