結果

問題 No.430 文字列検索
ユーザー kkktymkkktym
提出日時 2019-09-17 22:46:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 502 ms / 2,000 ms
コード長 890 bytes
コンパイル時間 646 ms
コンパイル使用メモリ 64,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 20:22:01
合計ジャッジ時間 6,747 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
typedef long long ll;
struct RollingHash{
  string a;
  int al;
  const ll B = 1e+9+7;
  
  RollingHash(string _a){a=_a;al=a.size();}

  int count_contain(string b)
  {
    int bl = b.size();
    if(al<bl) return 0;

    ll t = 1;
    rep(i,bl) t *= B;

    ll ah = 0,bh = 0;
    rep(i,bl) ah = ah*B + a[i];
    rep(i,bl) bh = bh*B + b[i];

    int res = 0;
    for(int i=bl;i<=al;++i){
      if(ah==bh) res++;
      if(i<al) ah = ah*B+a[i]-t*a[i-bl];
    }
    return res;
  }
};


int main()
{
  string s;
  cin >> s;
  int m;
  cin >> m;
  vector<string> c(m);
  rep(i,m) cin >> c[i];

  RollingHash roll(s);

  int res = 0;
  rep(i,m) res += roll.count_contain(c[i]);

  cout << res << "\n";

  
  return 0;
}
0