結果

問題 No.430 文字列検索
ユーザー oxyshower
提出日時 2019-02-03 00:53:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 457 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 1,500 ms
コンパイル使用メモリ 168,716 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-10 00:22:05
合計ジャッジ時間 6,634 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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