結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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;
}

vector<int> makeTable(string &s){
  int n = s.size();
  vector<int> ret(n+1);
  ret[0] = -1;
  int j = -1;
  for(int i=0; i<n; i++){
    while(j >= 0 && s[i] != s[j]) j = ret[j];
    ret[i+1] = ++j;
  }
  return ret;
}

vector<int> kmp(string &str, string &word){
  vector<int> table = makeTable(word), ret;
  int m = 0, i = 0, n = str.size();
  while(m + i < n){
    if(word[i] == str[m+i]){
      if(++i == (int)(word.size())){ 
        ret.emplace_back(m);
        m += i - table[i];
        i = table[i];
      }
    }
    else{
      m += i - table[i];
      if(i > 0) i = table[i];
    }
  }
  
  return ret;
}

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;
  }
  if (m == 1) {
    for (int i = 0; i < n; i++) {
      if (s[i] == t[0]) {
        cout << "-1\n";
        return 0;
      }
    }
  }
  auto res = kmp(s,t);
  int cur = 0, ans = 0;
  for (int i = 0; i < res.size(); i++) {
    if (cur <= res[i]) {
      cur = res[i] + m - 1;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
0