結果

問題 No.430 文字列検索
ユーザー oxyshoweroxyshower
提出日時 2019-02-03 00:53:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 502 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 1,625 ms
コンパイル使用メモリ 165,752 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 04:35:07
合計ジャッジ時間 7,685 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

const int B = 1e9+7, H = 1e9+9;
//Hash(C) = (C1*B^(n-1)+...+Cn*B^0) mod H
//aはbに含まれているか
int contain(string a,string b){
  if(a.size() > b.size()) return false;

  int t = 1,ah = 0,bh = 0;
  for(int i = 0; i < a.size(); i++) ah = ah*B + a[i],t *= B;
  for(int i = 0; i < a.size(); i++) bh = bh*B + b[i];

  int cnt = 0;
  for(int i = a.size(); i <= b.size(); i++){
    if(ah == bh) cnt++;
    if(i >= b.size()) break;
    bh = bh*B + b[i] - b[i-a.size()]*t;
  }
  return cnt;
}

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

  string s; cin >> s;
  int m; cin >> m;
  int ans = 0;
  for(int i = 0; i < m; i++){
    string c; cin >> c;
    ans += contain(c,s);
  }
  cout << ans << endl;

  return 0;
}
0