結果

問題 No.1005 BOT対策
ユーザー pyraninepyranine
提出日時 2020-12-21 16:08:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,324 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 172,368 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 11:43:15
合計ジャッジ時間 2,614 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

inline vector<int> z_algorithm(const string &s) {
  int n = (int)s.length();
  vector<int> z(n);
  z[0] = n;
  for (int i = 1, l = 0, r = 0; i < n; i++) {
    if (i <= r) z[i] = min(r - i + 1, z[i - 1]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
    if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
  }
  return z;
}

struct RollingHash {
  static const int base1 = 1007, base2 = 2009;
  static const int mod1 = 1000000007, mod2 = 1000000009;
  vector<long long> hash1, hash2, power1, power2;

  RollingHash(const string &S) {
    int n = (int)S.size();
    hash1.assign(n+1, 0);
    hash2.assign(n+1, 0);
    power1.assign(n+1, 1);
    power2.assign(n+1, 1);
    for (int i = 0; i < n; ++i) {
      hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;
      hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;
      power1[i+1] = (power1[i] * base1) % mod1;
      power2[i+1] = (power2[i] * base2) % mod2;
    }
  }
    
  inline pair<long long, long long> get(int l, int r) const {
    long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;
    if (res1 < 0) res1 += mod1;
    long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;
    if (res2 < 0) res2 += mod2;
    return {res1, res2};
  }

  inline int getLCP(int a, int b) const {
    int len = min((int)hash1.size()-a, (int)hash1.size()-b);
    int low = 0, high = len;
    while (high - low > 1) {
      int mid = (low + high) >> 1;
      if (get(a, a+mid) != get(b, b+mid)) high = mid;
      else low = mid;
    }
    return low;
  }

  inline int getLCP(const RollingHash &T, int a, int b) const {
    int len = min((int)hash1.size()-a, (int)hash1.size()-b);
    int low = 0, high = len;
    while (high - low > 1) {
      int mid = (low + high) >> 1;
      if (get(a, a+mid) != T.get(b, b+mid)) high = mid;
      else low = mid;
    }
    return low;
  }
};


int main() {
  string s, t;
  cin >> s >> t;
  int n = (int)s.size(), m = (int)t.size();
  if (n < m) {
    cout << "0\n";
    return 0;
  }
  
  RollingHash rhs(s), rht(t);
  int ans = 0;
  for (int i = 0; i <=n - m;) {
    if (rhs.get(i, i + m) == rht.get(0, m)) {
      if (m == 1) {
        cout << "-1\n";
        return 0;
      }
      ans++;
      i += m - 1;
    }
    else {
      i++;
    }
  }
  cout << ans << '\n';

  return 0;
}
0