結果

問題 No.430 文字列検索
ユーザー reiywreiyw
提出日時 2019-11-21 13:58:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,751 bytes
コンパイル時間 2,056 ms
コンパイル使用メモリ 172,684 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-04-18 02:09:24
合計ジャッジ時間 5,078 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region Macros
#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
#define IN(x, a, b) x >= a and x < b

using namespace std;
using ll = long long;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> constexpr T INF = numeric_limits<T>::max() / 2;
#pragma endregion

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  int M;
  cin >> M;
  int ans = 0;
  constexpr ll BASE = 26;
  const unordered_map<char, int> HASH = {
      {'A', 0},  {'B', 1},  {'C', 2},  {'D', 3},  {'E', 4},  {'F', 5},
      {'G', 6},  {'H', 7},  {'I', 8},  {'J', 9},  {'K', 10}, {'L', 11},
      {'M', 12}, {'N', 13}, {'O', 14}, {'P', 15}, {'Q', 16}, {'R', 17},
      {'S', 18}, {'T', 19}, {'U', 20}, {'V', 21}, {'W', 22}, {'X', 23},
      {'Y', 24}, {'Z', 25}};
  REP(i, M) {
    string C;
    cin >> C;
    if (SZ(C) > SZ(S)) {
      continue;
    }

    ll hash_q = 0;
    REP(n, SZ(C)) {
      ll m = SZ(C) - n - 1;
      hash_q += HASH.at(C[n]) * pow(BASE, m);
    }

    ll hash = 0;
    REP(n, SZ(C)) {
      ll m = SZ(C) - n - 1;
      hash += HASH.at(S[n]) * pow(BASE, m);
    }

    if (hash == hash_q) {
      ans++;
    }

    char prev = S[0];
    FOR(j, 1, SZ(S) - SZ(C) + 1) {
      hash -= HASH.at(prev) * pow(BASE, SZ(C) - 1);
      hash *= BASE;
      hash += HASH.at(S[j + SZ(C) - 1]);
      prev = S[j];
      if (hash == hash_q) {
        ans++;
      }
    }
  }
  cout << ans << endl;

  return 0;
}
0