結果

問題 No.430 文字列検索
ユーザー tonegawatonegawa
提出日時 2020-12-01 08:18:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,516 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 119,052 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2023-10-11 03:22:21
合計ジャッジ時間 5,236 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 TLE -
testcase_02 AC 1,219 ms
4,636 KB
testcase_03 AC 1,887 ms
4,488 KB
testcase_04 AC 3 ms
4,372 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,372 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 12 ms
4,424 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 1,375 ms
4,484 KB
testcase_17 AC 1,301 ms
4,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <random>
#include <string>
#include <cassert>
#include <array>
#include <map>
#include <functional>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

struct hash64{
private:
  const static ull BASE, MOD;
  vector<ull> INV;
  inline ull MUL(ull a, ull b){
    __int128_t t = __int128_t(a) * __int128_t(b);
    t = (t >> 61) + (t & MOD);
    return (t>=MOD?t-MOD:t);
  }
  inline ull ADD(ull a, ull b){
    return (a+b>=MOD?a+b-MOD:a+b);
  }
  inline ull SUB(ull a, ull b){
    return (a<b?(MOD-b)+a:a-b);
  }
public:
  hash64(int maxlen=3000000):INV(maxlen){
    ull ret = 1, num = BASE, b = MOD-2;
    INV[0] = 1;
    while(b>0){
      if(b&1) ret = MUL(ret, num);
      num = MUL(num, num);
      b /= 2;
    }
    for(int i=1;i<maxlen;i++) INV[i] = MUL(INV[i-1], ret);
  }
  //h[i]:=hash[0, i]  O(|S|)
  inline vector<ull> hashtable(string s){
    int n = s.size();
    vector<ull> ret(n);
    ull tmp = 0, x = 1;
    for(int i=0;i<n;i++){
      tmp = ADD(tmp, MUL(x, s[i]));
      x = MUL(x, BASE);
      ret[i] = tmp;
    }
    return ret;
  }
  //hash[l, r)  O(1)
  inline ull hash(const vector<ull> &v, int l, int r){
    if(l==r) return 0;
    return MUL(SUB(v[r-1], (l==0?0:v[l-1])), INV[l]);
  }
  //tの[l, r)の中にsの[a, b)(固定長)が含まれるか O(max(|r-l| - |b-a|, 0))
  inline bool contain(const vector<ull> &s, int a, int b, const vector<ull> &t, int l, int r){
    ull pat = hash(s, a, b);
    int len = b - a;
    for(int i=l;i<=r-len;i++){
      if(MUL(SUB(t[i+len-1], (i==0?0:t[i-1])), INV[i])==pat) {
        return true;
      }
    }
    return false;
  }
  //lcp(s[a, b), t[l, r))  O(log(len)), len := min(b-a, r-l)
  inline int lcp(const vector<ull> &s, int a, int b, const vector<ull> &t, int l, int r){
    int L = 0, R = min(b-a, r-l) + 1;
    while(R-L>1){
      int mid = (L+R)/2;
      if(hash(s, a, a+mid)==hash(t, l, l+mid)) L = mid;
      else R = mid;
    }
    return L;
  }
};
std::random_device seed_gen;
std::mt19937_64 engine(seed_gen());
const ull hash64::MOD((1ULL<<61)-1);
const ull hash64::BASE(engine()%hash64::MOD);

int main(){
  string s;std::cin >> s;
  hash64 h(100000);
  auto a = h.hashtable(s);
  int q;scanf("%d", &q);
  int n = s.size(), ans = 0;

  for(int i=0;i<q;i++){
    string t;std::cin >> t;
    auto b = h.hashtable(t);
    int m = t.size();
    for(int j=0;j<=n-m;j++){
      if(h.hash(a, j, j+m) == b[m-1]) {
        ans++;
      }
    }
  }
  printf("%d\n", ans);
}
0