結果

問題 No.430 文字列検索
ユーザー tonegawatonegawa
提出日時 2020-12-01 08:23:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,937 ms / 2,000 ms
コード長 2,862 bytes
コンパイル時間 1,353 ms
コンパイル使用メモリ 122,136 KB
実行使用メモリ 5,092 KB
最終ジャッジ日時 2023-10-11 03:22:59
合計ジャッジ時間 19,160 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 1,888 ms
4,520 KB
testcase_02 AC 988 ms
4,796 KB
testcase_03 AC 1,429 ms
4,704 KB
testcase_04 AC 3 ms
4,368 KB
testcase_05 AC 3 ms
4,368 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,368 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 3 ms
4,372 KB
testcase_10 AC 6 ms
4,372 KB
testcase_11 AC 1,926 ms
4,576 KB
testcase_12 AC 1,933 ms
4,464 KB
testcase_13 AC 1,911 ms
4,376 KB
testcase_14 AC 1,937 ms
4,372 KB
testcase_15 AC 1,904 ms
4,372 KB
testcase_16 AC 1,478 ms
5,092 KB
testcase_17 AC 987 ms
5,044 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;
  }
  //tの[l, r)の中にsの[a, b)(固定長)が含まれるか(始点のidxを返す) O(max(|r-l| - |b-a|, 0))
  inline vector<int> search(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;
    vector<int> ret;
    for(int i=l;i<=r-len;i++){
      if(MUL(SUB(t[i+len-1], (i==0?0:t[i-1])), INV[i])==pat) {
        ret.push_back(i);
      }
    }
    return ret;
  }
  //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 ans = 0;
  for(int i=0;i<q;i++){
    string t;std::cin >> t;
    ans += h.search(h.hashtable(t), 0, t.size(), a, 0, s.size()).size();
  }
  printf("%d\n", ans);
}
0