結果

問題 No.430 文字列検索
ユーザー kk
提出日時 2021-03-21 01:09:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 863 bytes
コンパイル時間 1,868 ms
コンパイル使用メモリ 203,488 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 10:59:32
合計ジャッジ時間 4,260 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 178 ms
5,376 KB
testcase_02 AC 175 ms
5,376 KB
testcase_03 AC 177 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 181 ms
5,376 KB
testcase_12 AC 179 ms
5,376 KB
testcase_13 AC 179 ms
5,376 KB
testcase_14 AC 182 ms
5,376 KB
testcase_15 AC 179 ms
5,376 KB
testcase_16 AC 176 ms
5,376 KB
testcase_17 AC 174 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

  string s;
  cin >> s;

  int n = s.length();
  vector<unsigned long long> h(n + 1);
  for (int i = 0; i < n; i++) {
    h[i+1] = h[i] * 1123 + (s[i] - 'A');
  }

  int ret = 0;
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    string t;
    cin >> t;
    unsigned long long g = 0;
    unsigned long long base = 1;
    for (int j = 0; j < t.length(); j++)
      g = g * 1123 + (t[j] - 'A');
    for (int j = 0; j < t.length(); j++)
      base *= 1123;
    
    for (int j = 0; j + t.length() <= n; j++) {
      if (h[j + t.length()] == base * h[j] + g) {
        bool ck = true;
        for (int k = 0; k < t.length(); k++)
          ck &= s[j+k] == t[k];
        if (ck)
          ++ret;
      }
    }
  }
  cout << ret << endl;
  
  return 0;
}
0