結果

問題 No.430 文字列検索
ユーザー tonegawatonegawa
提出日時 2020-12-01 08:28:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 2,523 bytes
コンパイル時間 1,523 ms
コンパイル使用メモリ 124,504 KB
最終ジャッジ日時 2025-01-16 11:06:46
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:87:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   87 |   int q;scanf("%d", &q);
      |         ~~~~~^~~~~~~~~~

ソースコード

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 ans = 0;
  map<ull, int> mp;
  for(int i=0;i<q;i++){
    string t;std::cin >> t;
    mp[h.hashtable(t)[t.size()-1]]++;
  }
  for(int l=1;l<=10;l++){
    for(int i=0;i<=int(s.size())-l;i++){
      ans += mp[h.hash(a, i, i+l)];
    }
  }
  printf("%d\n", ans);
}
0